Mongoose Document (iv) queries

Source: Internet
Author: User

Document can be retrieved through some model static helper methods.

Any model method that involves specifying a query condition can be executed in two ways.

When a callback function:

is passed, the operation executes immediately and the result is passed to the callback.

is not passed, returns a query instance, which provides a special query generation interface.

In Mongoose 4,Query has the then () function, so it can be used as a promise.

When a query is executed with a callback function, the request is specified as a JSON document. The syntax of the JSON document is the same as the MongoDB shell.

var person = Mongoose.model (' person ', yourschema); // find each person with a last name matching ' Ghost ', selecting the ' Name ' and ' occupation ' fields function (err, person) {  ifreturn  handleError (err);  Console.log (//  Space Ghost is a talk show host.})

The request is executed immediately and the result is passed to the callback. The mode used by all callbacks in the Mongoose: callback (Error, result). If an error occurs in the execution of the query, the error parameter contains an incorrect document and result is null. If the query succeeds, the error parameter will be Null,result will be the result of the request.

The callback is passed to the query anywhere in mongoose, and the callback follows the pattern callback (error, results). The result depends on the operator: FindOne () is a single document that may be empty, find () is a listof documents, COUNT () is the number of document , update () is the number of document affected. the model's API documentation details what is passed to the callback.

Now look at what happens without a callback:

//Find each person with a last name matching ' Ghost 'varquery = Person.findone ({' Name.last ': ' Ghost ') });//selecting the ' Name ' and ' occupation ' fieldsQuery.select (' name occupation ');//execute the query at a later timeQuery.exec (function(err, person) {if(ERR)returnHandleError (ERR); Console.log ('%s%s is a%s. ', Person.name.first, Person.name.last, person.occupation)//Space Ghost is a talk show host.})

In the above code, the query variable is of type query. Query allows you to use the link syntax to build a query instead of specifying a JSON object. The following two examples are equivalent.

//With a JSON docPerson . Find ({occupation:/host/,    ' Name.last ': ' Ghost ', Age: {$gt:, $lt: 66}, likes: {$inch: [' vaporizing ', ' talking '] }  }). Limit (10). Sort ({occupation:-1 }). Select ({name:1, Occupation:1 }).  exec (callback); //Using Query BuilderPerson . Find ({occupation:/host/ }). Where (' Name.last '). Equals (' Ghost '). Where (' Age '). GT (+) LT (66). Where (' Likes ').inch([' vaporizing ', ' talking ']). Limit (10). Sort ('-occupation '). Select (' Name occupation '). exec (callback);

There is a list of query helper functions in the API documentation.

1. Referencing other document

There is no joins in MongoDB but sometimes we still want to cite other collection's document. This is the origin of population. See how to include other collection document in the query results.

2. Stream

Queries can also be streamed from MongoDB to the application. Simply call the stream method of the query instead of exec to return an instance of Querysteam. Querystreams is a node. JS 0.8 Style input stream that is not a node. js 0.10 style.

Mongoose Document (iv) queries

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.