mongoose的常用操作汇总

本文汇总了Mongoose中常用的数据库操作方法,包括查询、创建、更新、删除等基本功能及高级查询技巧,如条件筛选、排序和限制结果数量等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分针网每日分享:mongoose的常用操作汇总

1.find查询: obj.find(查询条件,callback);
Model .find({},function(error,docs){
//若没有向find传递参数,默认的是显示所有文档
});
 
Model .find({ "age": 28 }, function (error, docs) {
if(error){
console .log("error :" + error);
}else{
console .log(docs); //docs: age为28的所有文档
}
});
 
2. Model.create(文档数据, callback))
Model .create({ name:"model_create", age:26}, function(error,doc){
if(error) {
console .log(error);
} else {
console .log(doc);
}
});
 
3. Entity.save(文档数据, callback))
var Entity = new Model({name:"entity_save",age: 27});
 
Entity .save(function(error,doc) {
if(error) {
console .log(error);
} else {
console .log(doc);
}
});
 
4. obj.update(查询条件,更新对象,callback);
var conditions = {name : 'test_update'};
 
var update = {$set : { age : 16 }};
 
TestModel .update(conditions, update, function(error){
if(error) {
console .log(error);
} else {
console .log('Update success!');
}
});
 
5. obj.remove(查询条件,callback);
var conditions = { name: 'tom' };
 
TestModel .remove(conditions, function(error){
if(error) {
console .log(error);
} else {
console .log('Delete success!');
}
});
 
6.使用$gt(>)、$lt(<)、$lte(<=)、$gte(>=)操作符进行排除性的查询,如下示例:
Model .find({"age":{"$gt":18}},function(error,docs){
//查询所有nage大于18的数据
});
 
Model .find({"age":{"$lt":60}},function(error,docs){
//查询所有nage小于60的数据
});
 
Model .find({"age":{"$gt":18,"$lt":60}},function(error,docs){
//查询所有nage大于18小于60的数据
});
 
7.$ne(!=)操作符的含义相当于不等于、不包含,查询时我们可通过它进行条件判定,具体使用方法如下:
Model .find({ age:{ $ne:24}},function(error,docs){
//查询age不等于24的所有数据
});
 
Model .find({name:{$ne:"tom"},age:{$gte:18}},function(error,docs){
//查询name不等于tom、age>=18的所有数据
});
 
8.和$ne操作符相反,$in相当于包含、等于,查询时查找包含于指定字段条件的数据。具体使用方法如下:
Model .find({ age:{ $in: 20}},function(error,docs){
//查询age等于20的所有数据
});
 
Model .find({ age:{$in:[20,30]}},function(error,docs){
//可以把多个值组织成一个数组
});
 
9.$or操作符,可以查询多个键值的任意给定值,只要满足其中一个就可返回,用于存在多个条件判定的情况下使用,如下示例:
Model .find({"$or":[{"name":"yaya"},{"age":28}]},function(error,docs){
//查询name为yaya或age为28的全部文档
});
 
10.$exists操作符,可用于判断某些关键字段是否存在来进行条件查询。如下示例
Model .find({name: {$exists: true}},function(error,docs){
//查询所有存在name属性的文档
});
 
Model .find({telephone: {$exists: false}},function(error,docs){
//查询所有不存在telephone属性的文档
});
 
11.结果排序:find(Conditions,fields,options,callback);
Model .find({},null,{sort:{age:-1}},function(err,docs){
//查询所有数据,并按照age降序顺序返回数据docs
});
 
12.限制数量:find(Conditions,fields,options,callback);
Model .find({},null,{limit:20},function(err,docs){
console .log(docs);
});
 
13.跳过数量:find(Conditions,fields,options,callback);
Model .find({},null,{skip:4},function(err,docs){
console .log(docs);
});
 
14.Schema添加属性值
var mongoose = require('mongoose');
 
var TempSchema = new mongoose.Schema;
 
TempSchema .add({ name: 'String', email: 'String', age: 'Number' });
 
  本文转自: http://www.f-z.cn/id/295

 

转载于:https://www.cnblogs.com/fenzhen/p/7210220.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值