首页 > 解决方案 > 如何循环MongoDB收集和处理文档?

问题描述

我想循环一个 MongoDB 集合并一个一个地处理每个文档。如果文档符合特定条件,我将更新文档以将其标记为“已处理”,并根据该文档插入或更新另一个集合。

这是我的代码,但它不起作用:

db.Chapter2.find( {} ).forEach(function(doc) {    
    var processed = true;
    if (doc._id = ObjectId("5a57c993d9dd9a41ead05a12")) {
      db.Chapter2.update( { _id: doc._id }, { $set: { "processed": processed } } ); 
      count = db.authors.find({ "Author": doc.Author });
      if ( count == 0) {
        db.authors.insertOne({Author: doc.Author, count: 1});
      } else {
        db.authors.updateOne({Author: doc.Author, {$set: {count: count +1}});
      }
    }
})

请注意:我不是在寻找聚合分辨率。

提前致谢。

理查德·许

标签: node.jsmongodb

解决方案


首先,您的代码在条件中有错误,应该==不是=.

至于一次处理 1 个文档,我建议使用异步库来满足此类需求。

这是如何将代码转换为使用异步库的示例

const async = require('async')
db.Chapter2.find({}, function(err, docs) {
    // docs is a collection of all of our documents, now we can use async.forEach to loop through them
    async.forEach(docs, function(doc, callback) {
        var processed = true;

        // doc is our current item in this collection, everytime we are done with it we would use callback so our loop moves on

        // Since you already have the doc on hand there is no need to call `update` you can just alter values and call `save`
        doc.processed = processed 

        // I suggest Author is an index in this collection to optimize it
        count = db.authors.find({ "Author": doc.Author });

        if ( count == 0) {
              db.authors.insertOne({Author: doc.Author, count: 1});
        } else {
              db.authors.updateOne({Author: doc.Author, {$set: {count: count +1}});
        }
        doc.save(function(err) {
           // In order for us to move to the next item we need to callback
           // the first argument is an error so here we simply check
           // If there is an error then callback with error otherwise without
           err ? callback(err) : callback(null)
        })

    }, function(err){
        // The loop is completed
        if (err) {
           // But there was an error handle it here
        } else {
           // The loop completed successfully
        }
    })
})

这个答案可以进一步优化。但这应该可以解决您的问题

var processed = true编辑:如果这个值永远不会改变,我不确定你为什么会有。头脑也只是设置doc.processed = true


推荐阅读