[英]find list of records if each records containt the value in nodejs and mongoose
[英]NodeJS and mongoose - find records from last hour
我正在查詢我的MongoDB,我想用2個表填充UI:
我可以獲取所有記錄,但是下面的當前記錄查詢不起作用。
app.get('/', function(req, res) {
var myDate = new Date(Date.now() - 1 * 60 * 60 * 1000);
var curr = MyCollection.find(
{_id: { $gt : myDate }}
).exec();
MyCollection.find({}, function(err, doc) {
res.render('index.jade', {latest: curr, all: doc}); //this query
});
});
我正在做{ _id: { $gt : myDate }}
但它不返回任何內容。
我究竟做錯了什么?
在這篇將時間戳記彈出到ObjectIds中的文章中 ,您需要首先將秒數從時間戳記轉換為十六進制字符串,如下所示:
var ObjectID = require('mongodb').ObjectID;
app.get('/', function(req, res) {
var timestamp = new Date(Date.now() - 1 * 60 * 60 * 1000);
var hexSeconds = Math.floor(timestamp/1000).toString(16);
// Create an ObjectId with that hex timestamp
var constructedObjectId = ObjectID(hexSeconds + "0000000000000000");
console.log(constructedObjectId); // prints 564cd3810000000000000000
MyCollection.find({}, function(err, doc) {
MyCollection.find({
"_id": { "$gt" : constructedObjectId }
}, function (err, curr) {
res.render('index.jade', { "latest": curr, "all": doc });
});
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.