我在线阅读'_id'包含创建时间的时间戳,您可以根据日期查询它。这是我到目前为止所尝试过的,但没有运气。
var d = new Date();
d.setDate(d.getDate() - 5);
collection.find( { }, { metal: 1, change: 1, percent: 1, _id: { $gte: d } } ).toArray(function(err, results) {
console.log(results);
});
Mongo中的数组:
[
{
"_id": "58e21b52524c0b0011fb92f4",
"metal": "Palladium",
"change": 3,
"percent": "+0.38%"
},
{
"_id": "58e21ee3524c0b0011fb92f5",
"metal": "Gold",
"change": 6,
"percent": "+0.54%"
},
{
"_id": "58e21ee3524c0b0011fb92f6",
"metal": "Silver",
"change": 0,
"percent": "+0.77%"
},
...
非常感谢@robertklep和我一起解决这个问题。如果其他人遇到这个问题,下面就是如何解决它的解决方案。
更新的答案:
const ObjectID = require('mongodb').ObjectID;
var d = new Date();
d.setDate(d.getDate() - 1); //1 would be 1day
var _id = ObjectID.createFromTime(d.getTime() / 1000);
collection.find({ _id : { $gte : _id }}, { metal: 1, change: 1, percent: 1 }).toArray(function(err, results) {
console.log(results);
...
});