Friday, January 30, 2015

MongoDB with Node.js

Find


this will:
 exports.connect = function() {  
      console.log("connecting to MongoDB ... ");  
      var mongoose = require('mongoose');  
      mongoose.connect('mongodb://host/db/');  
   
      var Gngram = mongoose.model('Schema', {   
           term: String,  
           noun: String,  
           verb: String }, 'collection');  
   
      Gngram.find(  
           {  
                $and:  
                [  
                     { "noun" : { $gte : "0.4" } },  
                     { "verb" : { $gte : "0.4" } }  
                ]  
           },  
           {  
                "term" : 1  
           },  
           function(err, gngrams) {  
                if (err) console.error(err);  
                console.log(gngrams);  
           }  
      );  
 }  

In the Google n-Gram collection this query will look for terms that are used as both nouns and verbs with a distribution spread of 40% each.  This code is highlighted in blue.

The query also uses projection to limit the returned data to terms sorted in ascending order.


Quick Reference

  • Specify in the sort parameter the fields to sort by and a value of 1 or -1 to specify an ascending or descending sort respectively[1]

No comments:

Post a Comment