2017-04-01 48 views
0
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); 
}); 

代码在mongodb和output中正常工作,但在nodejs中输出为空数组。无法查询MongoDB中两个日期之间的数据

+0

首先,使用'的console.log(ERR)',看看是否有错误 – krl

+1

不是字符串 –

回答

0

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); 
    } 
    }); 
+0

我用上面的代码中,你应该使用日期作为对象,但它给 的ReferenceError:ISODate没有定义 –

+0

我得到了这样的输出:服务器上运行的端口9000 日期函数调用 [] –

+0

@VijaykumarVijay使用'新日期',而不是'ISODate' – krl

相关问题