简体   繁体   中英

Unable to query data between two dates in MongoDB

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.

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. toArray

The 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);
    }
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM