Reputation: 71
db.test.find({"date":{$gte:"2017-04-11",$lt:"2017-04-13"}},function(err,doc){
console.log("date function called");
res.json(doc);
console.log(doc);
});
code works fine in mongodb and output,but in nodejs the output is empty array.
Upvotes: 0
Views: 1209
Reputation: 5296
Collections can be queried with find. The result for the query is actually a cursor object. This can be used directly or converted to an array. Making queries with find()
cursor.toArray(function(err, docs){})
converts the cursor object into an array of all the matching records. Probably the most convenient way to retrieve results but be careful with large datasets as every record is loaded into memory. toArrayThe mongo shell wraps objects of Date type with the ISODate helper; however, the objects remain of type Date. Return Date
var collection = db.collection('test');
collection.find({ "date": { $gte: new Date("2017-04-11"), $lt: new Date("2017-04-13") } })
.toArray(function (err, doc) {
console.log("date function called");
if (err) {
console.error(err);
} else {
console.log(doc);
res.json(doc);
}
});
Upvotes: 1