Mongoose Database Operations 3

Source: Internet
Author: User
Tags findone


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)

Cursor Handling:


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();});







































































Mongoose Database Operations 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.