Finding Documents by Using Mongoose

Finding documents by using the mongoose module is very similar to using the MongoDB Node.js native driver and yet very different in some ways. The concepts of logic operators, limit, skip, and distinct are all the same. However, there are two big differences. The first major difference is that when using Mongoose, the statements used to build the request can be piped together and reused because of the Query object, discussed earlier in this chapter. This allows Mongoose code to be much more dynamic and flexible when defining what documents to return and how to return them.

For example, these three queries are identical, just built in different ways:

var query1 = model.find({name:'test'}, {limit:10, skip:5, fields:{name:1,value:1}});
var query2 = model.find().where('name','test').limit(10).skip(5).
select({name:1,value:1});
var query3 = model.find();
query3.where('name','test'),
query3.limit(10).skip(5);
query3.select({name:1,value:1});

A good rule to follow when building a Query object using Mongoose is to add only things as you know you need in your code.

The second major difference is that MongoDB operations such as find() and findOne() return Document objects instead of JavaScript objects. Specifically, find() returns an array of Document objects instead of a Cursor object, and findOne() returns a single Document object. The Document objects allow you to perform the operations listed in Table 16.5.

Listing 16.3 illustrates several examples of the Mongoose way of retrieving objects from a database. The example in lines 10–14 counts the number of words that begin and end with a vowel. Then in line 15, the same Query object is changed to a find() operation, and limit() and sort() are added before execution in line 16.

The example in lines 22–32 uses mod() to find words with an even number of letters and more than six characters. Also, the output is limited to 10 documents, and each document returns only the word and size fields. Figure 16.3 shows the output of Listing 16.3.

Listing 16.3 mongoose_find.js: Finding documents in a collection by using Mongoose


01 var mongoose = require('mongoose'),
02 var db = mongoose.connect('mongodb://localhost/words'),
03 var wordSchema = require('./word_schema.js').wordSchema;
04 var Words = mongoose.model('Words', wordSchema);
05 setTimeout(function(){
06   mongoose.disconnect();
07 }, 3000);
08 mongoose.connection.once('open', function(){
09   var query = Words.count().where('first').in(['a', 'e', 'i', 'o', 'u']);
10   query.where('last').in(['a', 'e', 'i', 'o', 'u']);
11   query.exec(function(err, count){
12     console.log(" There are " + count +
13                 " words that start and end with a vowel");
14   });
15   query.find().limit(5).sort({size:-1});
16   query.exec(function(err, docs){
17     console.log(" Longest 5 words that start and end with a vowel: ");
18     for (var i in docs){
19       console.log(docs[i].word);
20     }
21   });
22   query = Words.find();
23   query.mod('size',2,0);
24   query.where('size').gt(6);
25   query.limit(10);
26   query.select({word:1, size:1});
27   query.exec(function(err, docs){
28     console.log(" Words with even lengths and longer than 6 letters: ");
29     for (var i in docs){
30       console.log(JSON.stringify(docs[i]));
31     }
32   });
33 });


Image

Figure 16.3 Finding documents in a collection by using Mongoose.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.74.160