Mongodb foreach用于嵌套集合以将文档更新/复制到另一个集合 [英] Mongodb foreach for nested collection to update/copy documents to another collection
问题描述
我们有以下格式的收藏集
We have collection in following format
{
"_id" : ObjectId("5640bdec1b988de0be31724e"),
"xyz" : "Toshiba Satellite Pro 4600 PIII800",
"Manufacture": "Toshiba"
"mappingData" : {
"title" : "xyz"
"brand" : "manufacture"
},
"_id" : ObjectId("5640bdec1b9435dfgdf43554b"),
"abc" : "Apple Ihone",
"mappingData" : {
"title" : "abc",
"brand" : "Company"
}
}
我想编写查询并期望查询结果来创建包含以下文档的另一个集合.
I want to write query and expecting query results to create another collection with following documents.
{
"title": "Toshiba Satellite Pro 4600 PIII800",
"Manufacture":"Toshiba"
}
{
"title": "Apple Ihone",
"Manufacture":"Apple"
}
我形成查询以获取预期结果,但返回错误.
I formed query to get expected results but returning error.
db.products.find().limit( 5 ).forEach(function(myDoc) {
var q = {};
myDoc.mappingData.array.forEach(function(doc){
q[doc]= myDoc[myDoc.mappingData.doc];
})
print(q);
//q will be inserted to new collection.
})
返回错误"TypeError: myDoc.mappingData.array has no properties (shell):3"
请帮助我解决此问题.
推荐答案
如果要更新整个集合,则不需要在当前代码中的光标上按limit
键.您收到的错误是因为products
集合中的mappingData
字段没有名为array
的子文档字段.在问题的示例中,只有title
子文档字段可用,而这正是您想要的字段.
If you are going to update the whole collection then the limit
on your cursor in your current code is not necessary. The error you are getting is because the mappingData
field in the products
collection does not have a subdocument field called array
. From your example in the question, only the title
subdocument field is available, and that's the one you want.
根据产品集合的大小,将转换后的文档插入到新集合中可能会影响您的操作.您可以使用新的无序
Depending on the size of the products collection, inserting the converted documents to a new collection may affect your operations. You can avoid slow insert performance with using the new unordered bulk insert API that streamlines your insert operations by sending them in bulk, and even better, it gives you real feedback about what succeeded and what failed.
以下批量插入API操作将在产品集合游标的 forEach()
The following bulk insert API operation will insert to newcollection
the desired data structure where the new documents are created within the products collection cursor's forEach()
loop, using the bracket notation to create new properties. In the bulk insert, you will be sending the operations to the server in batches of 1000 which gives you a better performance as you are not sending every request to the server, just once in every 1000 requests:
var bulk = db.newcollection.initializeUnorderedBulkOp(),
counter = 0;
db.products.find().forEach(function(doc) {
var document = {};
if (doc.mappingData.title) document["title"] = doc[doc.mappingData.title];
document["Manufacturer"] = doc.Manufacture;
bulk.insert(document);
counter++;
if (counter % 1000 == 0) {
bulk.execute();
bulk = db.newcollection.initializeUnorderedBulkOp();
}
});
if (counter % 1000 != 0) { bulk.execute(); }
在上面的示例中,您从批量API操作获得的反馈将采用以下格式:
With the above example, the feedback you get from the bulk API operation would be in this format:
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
查询新集合db.newcollection.find()
将产生:
/* 0 */
{
"_id" : ObjectId("56558b0427adb60c9f7e6f8d"),
"title" : "Toshiba Satellite Pro 4600 PIII800",
"Manufacturer" : "Toshiba"
}
/* 1 */
{
"_id" : ObjectId("56558b0427adb60c9f7e6f8e"),
"title" : "Apple Ihone",
"Manufacturer" : undefined
}
这篇关于Mongodb foreach用于嵌套集合以将文档更新/复制到另一个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!