node.js - 如何循环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}});
}
}
})
请注意:我不是在寻找聚合分辨率。
提前致谢。
理查德·许
解决方案
首先,您的代码在条件中有错误,应该==
不是=
.
至于一次处理 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
推荐阅读
- three.js - 场景变化时 Aframe a-sky 变化旋转
- javascript - 我应该在哪里嵌入 htmlspecialchars,为什么?
- python - 是什么导致 Django 中出现此错误:NOT NULL 约束失败:mainapp_comment.post_id
- machine-learning - 标准化测试集但范围更高
- java - 任务调度程序运行.exe问题(.exe包含处理java代码)
- php - 如何从列中切片数据并在codeigniter中根据它进行排序?
- python-3.x - 有哪些不同类型的数据类型?
- reactive-programming - 在组成反应链时,我应该依赖“单项”还是“普通项”参数?
- spring - 此应用程序没有显式映射 /error,因此您将其视为后备。- 春季启动 - jsp
- sql - 合并来自不同查询的两个虚拟表