我正在尝试使用websockets将node.js/MongoDB实例中的数据"流"到客户端.一切运作良好.
但是如何识别结果中的最后一个文档?我正在使用node-mongodb-native从node.js连接到MongoDB.
一个简化的例子:
collection.find({}, {}, function(err, cursor) {
if (err) sys.puts(err.message);
cursor.each(function(err, doc) {
client.send(doc);
});
});
Run Code Online (Sandbox Code Playgroud)
And*_*ich 29
由于mongodb objectId contatins创建日期,您可以按id排序,降序然后使用limit(1):
db.collection.find().sort( { _id : -1 } ).limit(1);
Run Code Online (Sandbox Code Playgroud)
注意:我根本不熟悉node.js,上面的命令是mongo shell命令,我想你可以轻松地将它重写为node.js.
| 归档时间: |
|
| 查看次数: |
16493 次 |
| 最近记录: |