【发布时间】:2015-05-13 09:37:23
【问题描述】:
我想使用节点 js 在 mongodb 表中查找文档。 我目前正在使用 mongojs 插件。
这是我遇到的问题:
- 我连接到 DB。
- 我得到了当前时间戳
- 我想每 10 秒打印一次在这 10 秒内添加的所有元素。
var timestamp = new Date().getTime();
console.log('timestamp to compare: ' + timestamp);
setInterval(function() {
var x = db.collection.find({'create_time' : {$gt : timestamp}}).toArray(function(err, entity) {
console.log(entity);
});
console.log('checking...')
timestamp = new Date().getTime();
console.log('timestamp to compare: ' + timestamp);
}, 10000);
不知何故,我没有得到任何结果。您可以在下面看到命令提示符输出。 http://s11.postimg.org/a8cnffedf/2015_03_11_1521.png
我会感谢任何帮助。谢谢。
【问题讨论】:
-
哦,在 var x .... console.log('checking...') 我在打印的地方放了一些代码集合中的最后一个文档。它在附加网址下的屏幕上可见
-
为什么不搜索
current_timestamp - 10类似:timestamp = new Date().getTime() - (10 * 1000); var x = db.collection.find({'create_time' : {$gt : timestamp}}).toArray(function(err, entity) { console.log(entity); }); -
@Lelas 您是在 Mongo 中将时间戳存储为 ISO 字符串还是整数?
-
ISO 字符串...我添加了引号,现在我收到了一些结果,但是这些不是在时间戳变量中存储时间之后添加的实体...时间戳的格式是否正确?还是应该乘以/除以某个值?
标签: javascript node.js mongodb timestamp mongojs