Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如何使用upsert_Mongodb_Mongodb Query - Fatal编程技术网

Mongodb 如何使用upsert

Mongodb 如何使用upsert,mongodb,mongodb-query,Mongodb,Mongodb Query,我收集了像这样的文件 [ { _id : ObjectId("xxxxxx") , u_id : 5 , name : "E" , comment : [1,2] }, { _id : ObjectId("yyyyyy") , u_id : 4 , name : "D" , comment : [] }, { _id : ObjectId("zzzzzz") , u_id : 3 , name : "C" , comment : [1,2] }, { _id :

我收集了像这样的文件

[
    { _id : ObjectId("xxxxxx") , u_id : 5 , name : "E" , comment : [1,2] },
    { _id : ObjectId("yyyyyy") , u_id : 4 , name : "D" , comment : [] },
    { _id : ObjectId("zzzzzz") , u_id : 3 , name : "C" , comment : [1,2] },
    { _id : ObjectId("aaaaaa") , u_id : 2 , name : "B" , comment : [1,2] },
    { _id : ObjectId("bbbbbb") , u_id : 1 , name : "A" , comment : [1] },
]
var multi_document =
[
    { u_id : 8 , name : "H" , comment : [1,2] }, //Insert new document
    { u_id : 7 , name : "G" , comment : [] }, //Insert new document
    { u_id : 6 , name : "F" , comment : [1,2] }, //Insert new document
    { u_id : 5 , name : "E" , comment : [1,2,3] }, //update [1,2] to [1,2,3]
    { u_id : 4 , name : "DD" , comment : [1] }, //update D to DD and [] to [1]
    { u_id : 3 , name : "C" , comment : [1,2] }, //not do anything it same original
];
现在我有一系列文档准备插入或更新到这个集合中,如下所示

[
    { _id : ObjectId("xxxxxx") , u_id : 5 , name : "E" , comment : [1,2] },
    { _id : ObjectId("yyyyyy") , u_id : 4 , name : "D" , comment : [] },
    { _id : ObjectId("zzzzzz") , u_id : 3 , name : "C" , comment : [1,2] },
    { _id : ObjectId("aaaaaa") , u_id : 2 , name : "B" , comment : [1,2] },
    { _id : ObjectId("bbbbbb") , u_id : 1 , name : "A" , comment : [1] },
]
var multi_document =
[
    { u_id : 8 , name : "H" , comment : [1,2] }, //Insert new document
    { u_id : 7 , name : "G" , comment : [] }, //Insert new document
    { u_id : 6 , name : "F" , comment : [1,2] }, //Insert new document
    { u_id : 5 , name : "E" , comment : [1,2,3] }, //update [1,2] to [1,2,3]
    { u_id : 4 , name : "DD" , comment : [1] }, //update D to DD and [] to [1]
    { u_id : 3 , name : "C" , comment : [1,2] }, //not do anything it same original
];
我可以使用
db.collection.update(多文档)
?如果没有,我该怎么办

这是预期的结果:

[
    { _id : ObjectId("db_created") , u_id : 8 , name : "H" , comment : [1,2] },
    { _id : ObjectId("db_created") , u_id : 7 , name : "G" , comment : [] },
    { _id : ObjectId("db_created") , u_id : 6 , name : "F" , comment : [1,2] },
    { _id : ObjectId("xxxxxx") , u_id : 5 , name : "E" , comment : [1,2,3] },
    { _id : ObjectId("yyyyyy") , u_id : 4 , name : "DD" , comment : [1] },
    { _id : ObjectId("zzzzzz") , u_id : 3 , name : "C" , comment : [1,2] },
    { _id : ObjectId("aaaaaa") , u_id : 2 , name : "B" , comment : [1,2] },
    { _id : ObjectId("bbbbbb") , u_id : 1 , name : "A" , comment : [1] },
]
您可以使用Bulk()

使用有序操作列表,MongoDB连续执行列表中的写操作。如果在处理其中一个写入操作期间发生错误,MongoDB将返回,而不处理列表中的任何剩余写入操作


关于批量API的有趣博客文章:

最好的方法是使用API

首先,您需要在
multi_文档
数组上循环,对于在集合中找到的每个文档,都需要具有相同
u id的文档
。为此,我们需要使用将
upsert
设置为
true
的方法,然后使用指定要更新的字段的方法,这里是
comment
。在更新文档中,您需要使用运算符确保注释字段和修改器中没有重复项,因为
注释
是一个数组

var bulk=db.collection.initializeOrderedBulkOp(),
计数=0;
multi_document.forEach(函数(doc){
bulk.find({“u_id”:doc.u_id})
.upsert()
.更新({
“$set”:{“name”:doc.name},
“$addToSet”:{“comment”:{“$each”:doc.comment}
}); 
计数++;
如果(计数%1000==0){
//每1000次操作执行一次并重新初始化。
bulk.execute();
bulk=db.collection.initializeOrderedBulkOp();
} 
})
//清理队列
如果(计数%1000!=0)
bulk.execute();

{u_id:4,名称:“DD”,注释:[]},//将D更新为DD,将[]更新为[1]
此处设置了哪些标准?它应该是
{u_id:4,名称:“DD”,注释:[]}
它正在工作。但是结果并没有将D更新为DD(它创建了新的文档和错误,因为我使用u_id作为唯一的)BulkWriteError({“writeErrors”):[{“index”:4,“code”:11000,“errmsg”:“E11000重复键错误索引:db.collection.$u_id dup key:{:4.0}”,“op”:{“q”:{“u_id”:4,“name”:“DD”},“u”:{“$addToSet”:{“comment”:{“$each”:[]}}},“multi”:true,“upsert”:true}}],“nInserted”:0,“nUpserted”:3,“nmmatched”:2,“nModified”:1,})谢谢您的帮助。我也会检查它。@user3100115