gpt4 book ai didi

mongodb - 从 MongoDB 中的数组中删除多个文档

转载 作者:可可西里 更新时间:2023-11-01 09:16:55 25 4
gpt4 key购买 nike

我的文档包含一个像这样的数组:

{
"differentialDiagnosis" : "IART/Flutter",
"explanation" : "The rhythm.",
"fileName" : "A115a JPEG.jpg",
"history" : "1 year old with fussiness",
"interpretationList" : [
{
"interpretations" : [
ObjectId("54efe7c8d6d5ca3d5c580a22"),
ObjectId("54efe80bd6d5ca3d5c580a26")
]
},
{
"interpretations" : [
ObjectId("54efe80bd6d5ca3d5c580a26"),
ObjectId("54efe82ad6d5ca3d5c580a28")
]
}
],
}

我想删除所有出现的 ObjectId("54efe80bd6d5ca3d5c580a26"),但我写了一个查询:

db.ekgs.update({'interpretationList.interpretations':ObjectId("54c09fb3581c4c8c218d1a40")}, {$pull:{ 'interpretationList.$.interpretations':{ ObjectId("54c09fb3581c4c8c218d1a40")}})

仅删除第一次出现ObjectId("54efe80bd6d5ca3d5c580a26")

最佳答案

您的查询仅删除第一次出现的原因是因为,如 this page 中所述在文档中,“位置 $ 运算符充当与查询文档匹配的第一个元素的占位符”。

问题在于,使用在嵌入式数组中的嵌入式对象中具有嵌入式数组的架构来处理这些类型的更新确实很棘手。为了解决这个问题,如果您能够扁平化架构,那么您的更新就会变得容易得多。因此,如果您的文档看起来像这样:

{
"differentialDiagnosis" : "IART/Flutter",
"explanation" : "The rhythm.",
"fileName" : "A115a JPEG.jpg",
"history" : "1 year old with fussiness",
"interpretations" : [
ObjectId("54efe7c8d6d5ca3d5c580a22"),
ObjectId("54efe80bd6d5ca3d5c580a26"),
ObjectId("54efe82ad6d5ca3d5c580a28")
]
}

那么您的查询将像下面的查询一样简单。 (如果您想更新多个文档,请记住添加 { "multi": true } 作为选项)。

db.ekgs.update(
{ "interpretations": ObjectId("54efe80bd6d5ca3d5c580a26")},
{ "$pull": { "interpretations": ObjectId("54efe80bd6d5ca3d5c580a26") }}
);

但我知道您可能无法更改架构。在这种情况下,您可以尝试需要一个小脚本的解决方案。在 mongo shell 中,您可以使用以下 JavaScript 代码来执行操作。

// Get cursor with documents requiring updating.
var oid = ObjectId("54efe80bd6d5ca3d5c580a26");
var c = db.ekgs.find({ "interpretationList.interpretations": oid });

// Iterate through cursor, removing oid from each subdocument in interpretationList.
while (c.hasNext()) {
var isModified = false;
var doc = c.next();
var il = doc.interpretationList;
for (var i in il) {
var j = il[i].interpretations.length;
while (j--) {

// If oid to remove is present, remove it from array
// and set flag that the document has been modified.
if (il[i].interpretations[j].str === oid.str) {
il[i].interpretations.splice(j, 1);
isModified = true;
}
}
}

// If modified, update interpretationList for document.
if (isModified) {
db.ekgs.update({ "_id": doc._id }, { "$set": { "interpretationList": il }});
}
}

更新:使用 Node.js 驱动程序如何工作的示例。

// Get cursor with documents requiring updating.
var oid = new ObjectID("54efe80bd6d5ca3d5c580a26");
var ekgs = db.collection("ekgs");
ekgs.find({ "interpretationList.interpretations": oid },
function(err, c) {

if(err) throw err;

// Iterate through cursor, removing oid from each subdocument in interpretationList.
c.each(function(err, doc) {
if (err) throw err;

// If doc is null then the cursor is exhausted/empty and closed.
if (doc != null) {
var isModified = false;
var il = doc.interpretationList;
for (var i in il) {
var j = il[i].interpretations.length;
while (j--) {

// If oid to remove is present, remove it from array
// and set flag that the document has been modified.
if (il[i].interpretations[j].equals(oid)) {
il[i].interpretations.splice(j, 1);
isModified = true;
}
}
}

// If modified, update interpretationList for document.
if (isModified) {
ekgs.update({ "_id": doc._id },
{ "$set": { "interpretationList": il }},
function(err, res) {

if (err) throw err;

// Callback.
console.log(res);
});
}
}
});
});

关于mongodb - 从 MongoDB 中的数组中删除多个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28782909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com