mongoose CRUD
mongoose 的增删改查
- 添加 Model.create(文档数据, callback))
Model.create({ name:"model_create", age:26}, function(error,doc){ if(error) { console.log(error); } else { console.log(doc); } }); - 删除 obj.remove(查询条件,callback);
var conditions = { name: 'tom' }; TestModel.remove(conditions, function(error){ if(error) { console.log(error); } else { console.log('Delete success!'); } });
-
更新 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!'); } });
-
查询 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的所有文档 } });

浙公网安备 33010602011771号