在MongoDB上,当我的回调位于“查找"内部时,如何限制查询? [英] On MongoDB how can I limit the query, when my callback is inside "find"?
本文介绍了在MongoDB上,当我的回调位于“查找"内部时,如何限制查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在MongoDB中有这个查询
I have this query in MongoDB
db.privateMessages.find(
{ $or : [
{fromId: userId, toId: socket.userId} ,
{fromId: socket.userId, toId: userId} ]
},
function(err, messages) { pushSvdMsgs(messages); });
除了我得到50个结果的事实之外,它都能完美运行.
It works perfectly, except for the fact that I get 50 results.
我已经尝试过了:
db.privateMessages.find( { $or : [ {fromId: userId, toId: socket.userId} , {fromId: socket.userId, toId: userId} ] }, function(err, messages) { pushSvdMsgs(messages); }).limit(10);
但这也无济于事,所以我在下面尝试了一下,但也无济于事.
But that didn't help either, so I tried this below which also didn't help limit it.
db.privateMessages.find( { $or : [ {fromId: userId, toId: socket.userId} , {fromId: socket.userId, toId: userId} ] }, { $limit : 2 }, function(err, messages) { pushSvdMsgs(messages); });
如何限制此查询的结果数量,并仍然以与我相同的方式调用回调?
How can I limit the number of results from this query, and still call the callback the same way I have?
推荐答案
您几乎完全正确.试试这个:
You got it almost right. Try this one:
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)
.我们需要一个空对象来使驱动程序正确解释选项.
The syntax is find(query, fields, options)
. We need that empty object to make driver interpret options correctly.
这篇关于在MongoDB上,当我的回调位于“查找"内部时,如何限制查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文