首页 > 代码库 > mongoose 数据库操作3
mongoose 数据库操作3
Model.find(query, fields, options, callback)
Model.find({ ‘some.value‘: 5 }, function (err, docs) {
// docs is an array
});
Model.findOne
Model.findOne({ age: 5}, function (err, doc){
// doc is a Document
});
Model.findById
Model.findById(obj._id, function (err, doc){
// doc is a Document
});
Model.count
Model.count(conditions, callback);
Model.remove
Model.remove(conditions, callback);
Model.distinct
Model.distinct(field, conditions, callback);
Model.where
Model
.where(‘age‘).gte(25)
.where(‘tags‘).in([‘movie‘, ‘music‘, ‘art‘])
.select(‘name‘, ‘age‘, ‘tags‘)
.skip(20)
.limit(10)
.asc(‘age‘)
.slaveOk()
.hint({ age: 1, name: 1 })
.exec(callback);
Model.$where
Model.$where(‘this.firstname === this.lastname‘).exec(callback)
var query = Model.find({});
query.where(‘field‘, 5);
query.limit(5);
query.skip(100);
query.exec(function (err, docs) {
// called when the `query.complete` or `query.error` are called
// internally
});
Model.update
var conditions = { name: ‘borne‘ }// 条件
, update = { $inc: { visits: 1 }}//修改
, options = { multi: true };//选项 multi 修改多个
Model.update(conditions, update, options, callback);
function callback (err, numAffected) {
// numAffected is the number of updated documents
})
Model.findOne({ name: ‘borne‘ }, function (err, doc){//doc 是模型
doc.name = ‘jason borne‘;
doc.visits.$inc();
doc.save();
});
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。