2017-04-01 49 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

相關問題