Read Mongoose API Record

Source: Internet
Author: User

Mongoose need to be used on schemas basis

var mongoose = require (' mongoose ');

var Schema = Mongoose. Schema;

var Blogschema = New Schema ({

Title:string, Author:string,

Body:string, Comments: [{body:string, date:date}],

Date: {type:date, Default:Date.now},

Hidden:boolean,

Meta: {votes:number, Favs:number}

});

Create a module

var blog = mongoose.model (' blog ', Blogschema);

Managing the form of a database

var db = Mongoose.connect (' mongodb://localhost/test ');

Mongoose Common query statements

Provides find, FindOne, FindByID for document queries

Model.find (Query,fields,options,callback)//fields,options is an optional parameter

Simple query: Model.find ({' Cursor ': 6},function (Err,doc) {})//doc is the statement after the query

Query only the result of the specified key: Model.find ({},{' first ', ' Last '},function (Err,docs) {})//doc is the only one and last key value that contains the document

Model.findone is the same as find but returns only a single document.

Model.findbyid and find are the same, but it only accepts the document's _id as a parameter, returning a single document

Model.findbyid (Obj._id,function (Err,doc) {})

Model.count returns the number of documents that match the criteria

Model.count (Conditions,callback)

Model.remove Deleting a document that matches a condition

Model.remove (Contions,callback)

Model.where queries are more complex when using where

Modle. Where (' age '). GTE (25)

. where (' tags '). in{[' movie ', ' Music ', ' art '}

. Select (' Name ', ' age ', ' tags '}

. Skip (20)

. Limit (10)

. ASC (' age ')

. Slaveok ()

...

Model.update Updating the database

var contions = {name: ' Borne '},

Update = {$inc: {visits:1}},

Options = {Multi:true}

Model.update (Conditions,update,options,callback)

Read Mongoose API Records

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.