mongodb - mongodb如何批量获取数据

标签 mongodb mongodb-query monk

我想从 MongoDB 中检索数据,一次 5 个

我正在使用 limit 来限制返回的记录数

router.post('/List', function (req, res) {
    var db = req.db;
    var collection = db.get('clnName');
    collection.find({}, { limit: 5 * req.body.requestCount }, function (e, docs) {
        res.json(docs);
    });
});

在这里,我从客户端递增 requestCount 变量,以便我获得 5 的倍数的数据。 我想要实现的是在第一个请求中获取前 5 个数据,在第二个请求中获取接下来的 5 个数据,但实际情况是,我获取了前 5 个数据,然后是然后是前 10 个数据

我应该做出什么改变来实现我的需要?

将使用batch size in mongo cursor methods解决我的问题?

最佳答案

很明显,这里的一个明显案例是使用 .skip() 作为修饰符以及 .limit() 以实现数据的“分页”:

    collection.find({}, { "limit": 5, "skip": 5 * req.body.requestCount  }, function 

但如果您只是批量处理,更好的办法是过滤掉您已经看到的范围。 _id 字段是一个很好的标识符,无需其他排序。所以在第一个请求上:

var lastSeen = null;
    collection.find(
        {}, 
        { "limit": 5, "sort": { "_id": 1}  },
        function(err,docs) {
           docs.forEach(function(doc) {
               // do something
               lastSeen = doc._id;        // keep the _id
           });
        }
    );

下一次将“lastSeen”存储在类似 session 变量(或其他只处理批处理的循环结构)中:

    collection.find(
        { "_id": { "$gt": lastSeen }, 
        { "limit": 5, "sort": { "_id": 1}  },
        function(err,docs) {
           docs.forEach(function(doc) {
               // do something
               lastSeen = doc._id;        // keep the _id
           });
        }
    );

因此排除小于最后看到的 _id 值的所有结果。

对于其他排序,这仍然是可能的,但您需要记下最后看到的 _id 和最后排序的值。自上次值更改以来,还保持 _id 被视为列表。

    var lastSeenIds = [],
        lastSeenValue = null;    

    collection.find(
        {}, 
        { "limit": 5, "sort": { "other": 1, "_id": 1 }  },
        function(err,docs) {
           docs.forEach(function(doc) {
               // do something
               if ( lastSeenValue != doc.other ) {  // clear on change
                   lastSeenValue = doc.other;
                   lastSeenIds = [];
               }
               lastSeenIds.push(doc._id);     // keep a list
           });
        }
    );

然后在您的下一次迭代中使用适当的变量:

    collection.find(
        { "_id": { "$nin": lastSeenIds }, "other": { "$gte": lastSeenValue } },
        { "limit": 5, "sort": { "other": 1, "_id": 1 }  },
        function(err,docs) {
           docs.forEach(function(doc) {
               // do something
               if ( lastSeenValue != doc.other ) {  // clear on change
                   lastSeenValue = doc.other;
                   lastSeenIds = [];
               }
               lastSeenIds.push(doc._id);     // keep a list
           });
        }
    );

这比“跳过”符合基本查询条件的结果要高效得多。

关于mongodb - mongodb如何批量获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31826760/

相关文章:

javascript - 在 MongoDB 中显示文档字段时出现问题

node.js - 具有异步回调的单元测试快速路由

mongodb - 或者使用 If 和 In mongodb

python - 使用 pymongo 将 JSON 导入 mongoDB

mongodb - 如何计算匹配的数组元素

javascript - 集合中对象的 Mongo/Monk 计数

node.js - 简单 mongo/monk findAndModify 查询的问题

javascript - Node JS : Not inserting into mongodb

javascript - MongoDB 中插入失败

node.js - 我可以动态地将索引添加到 Mongoose 中的架构中吗?