【问题标题】:MongoJS find() returning only 10 results?MongoJS find() 只返回 10 个结果?
【发布时间】:2014-08-12 18:08:23
【问题描述】:

我是 NodeJS 的新手,我正在使用 MongoJS 连接到我的数据库。

一切正常,但是当我使用 db.collection.find() 函数时,它只返回数据库中的前 10 个结果。

function listMessages(from,to){
console.log("[REQ] User "+from+" asked chat with "+to);
var htext = 'Welcome to the chat<br/>';
db.messages2.find({$or:[{from_id: from, to_id: to}, {from_id: to, to_id: from}]},function(err, echoData) { 
  if( err || !echoData) console.log("No messages found");
  else echoData.forEach( function(returnData) {
  htext += returnData.from_id+": "+returnData.text+"<br/>";
  });
  io.sockets.emit('html message '+from, htext, to);
});
}

它工作正常,但只返回数据库中的前 10 个结果,即使我使用了限制。
我也试过用这个,没用:

db.messages2.find({$or:[{from_id: from, to_id: to}, {from_id: to, to_id: from}]},{},{limit: 50}, function(err, echoData) {

我需要做什么来解决这个问题?谢谢!

【问题讨论】:

    标签: javascript node.js mongodb socket.io mongojs


    【解决方案1】:

    mongoDB 中的默认限制是 10

    来自文档:

    collection.find(query[[[, fields], options], callback]);

    options - 定义额外的逻辑(排序选项、分页等)

    分页 分页可以通过选项参数limit和skip实现

    所以有了新的限制:

    db.messages2.find({}, {}, {limit: 1000}, function(err, echoData) {
    

    【讨论】:

    • 我试过了,它仍然只返回数据库中的前 10 个结果。
    • 您确定您有超过 10 条匹配记录吗?尝试删除您的查询条件。
    • 是的,它有大约 40 条记录。我删除了 or 并只搜索 {from_id: from, to_id: to}。它工作正常,但在 $or 条件下错误又回来了。
    • 它现在有效,我的 from/to 变量有错误。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    相关资源
    最近更新 更多