Mongo Find Query in Node.js

Write a Find Query  in node.js 

Sample Document:

       "_id" : ObjectId("602f2db6376b261d1d6addc2"), 
       "id" : 1.0
       "pupil" : "John"
       "std" : 6.0
       "ht" : 153.0
       "wt" : 43.0
   }
   { 
       "_id" : ObjectId("602f2db6376b261d1d6addc3"), 
       "id" : 2.0
       "pupil" : "Jack"
       "std" : 6.0
       "ht" : 164.0
       "wt" : 54.0
   }
   { 
       "_id" : ObjectId("602f2db6376b261d1d6addc4"), 
       "id" : 3.0
       "pupil" : "Jill"
       "std" : 6.0
       "ht" : 173.0
       "wt" : 69.0
   }
   { 
       "_id" : ObjectId("602f2db6376b261d1d6addc5"), 
       "id" : 4.0
       "pupil" : "william"
       "std" : 6.0
   }
   

Approach

Query: Query to find a student with wt greater than 50.0 in node.js.

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(urlfunction(errdb) {
  if (errthrow err;
  var dbo = db.db("BecomeCodeExpert");
  var query = { "wt" : {"$gt":50.0 }};
  dbo.collection("students").find(query).toArray(function(errresult) {
    if (errthrow err;
    console.log(result);
    db.close();
  });
});

Output: 

[ { _id: 602f2db6376b261d1d6addc3,
       id: 2,
       pupil: 'Jack',
       std: 6,
       ht: 164,
       wt: 54 },
     { _id: 602f2db6376b261d1d6addc4,
       id: 3,
       pupil: 'Jill',
       std: 6,
       ht: 173,
       wt: 69 
} ]
 

No comments:

Post a Comment