我在 MongoDB 中有这个查询
db.privateMessages.find(
{ $or : [
{fromId: userId, toId: socket.userId} ,
{fromId: socket.userId, toId: userId} ]
},
function(err, messages) { pushSvdMsgs(messages); });
它工作得很好,除了我得到 50 个结果。
我试过这个:
db.privateMessages.find( { $or : [ {fromId: userId, toId: socket.userId} , {fromId: socket.userId, toId: userId} ] }, function(err, messages) { pushSvdMsgs(messages); }).limit(10);
但这也没有帮助,所以我在下面尝试了这个,也没有帮助限制它。
db.privateMessages.find( { $or : [ {fromId: userId, toId: socket.userId} , {fromId: socket.userId, toId: userId} ] }, { $limit : 2 }, function(err, messages) { pushSvdMsgs(messages); });
如何限制此查询的结果数量,并且仍然以与我相同的方式调用回调?
最佳答案
你几乎是对的。试试这个:
db.privateMessages.find( { $or : [ {fromId: userId, toId: socket.userId} ,
{fromId: socket.userId, toId: userId} ] },
{},
{ limit : 2 },
function(err, messages) { pushSvdMsgs(messages); });
语法是find(query, fields, options)
。我们需要那个空对象来让驱动程序正确解释选项。
关于node.js - 在 MongoDB 上,当我的回调在 "find"内时,如何限制查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9970443/