从嵌套文档数组 mongodb 更新特定元素,其中有两个匹配项 [英] update specific element from nested document array mongodb where has two matches
问题描述
如果不存在,我需要更新或创建特定对象,设置 score.b1 =50 和 total=100,其中对象匹配 curse=5 block=2
{ "_id":"sad445";年":2020,等级":4,部分":A",id":100,名称":佩德罗",注释":[{诅咒":5,块":1,分数":{ a1":5,a2":10,a3":15},总": 50},{诅咒":5,块":2,分数":{ b1":10,b2":20,b3":30},总": 20}]}
我可以更新所有 obj,但我需要更新或从分数中创建特定元素,而不是全部.和/或创建对象notes":[{curse, block and score}] 如果notes 是空的notes:[]
notas.UpdateMany({"$and":[{"_id":"sad445"},{"notes":{"$elemMatch":{"curse":5,"block":3}}}]},{$set":{updated_at":{$date":{$numberLong":1620322881360"}},"notes.$.score":{"vvkzo":15,"i2z4i":2,"i2z4i|pm":5},notes.$.total":100}},{多个":假})
Demo - https://mongoplayground.net/p/VaE28ujeOPx
使用$(更新)
<块引用>位置 $ 运算符标识数组中要更新的元素,而无需明确指定该元素在数组中的位置.
<块引用>
位置 $ 运算符充当与查询文档匹配的第一个元素的占位符,并且
<块引用>
数组字段必须作为查询文档的一部分出现.
db.collection.update({注释":{"$elemMatch": { "block": 2, "curse": 5 }}},{$set: { "notes.$.score.b4": 40 }})
<小时>
阅读upsert:真实
<块引用>可选.当为真时,update() 要么:
如果没有文档与查询匹配,则创建一个新文档.更多详细信息请参见 upsert 行为.更新匹配的单个文档查询.如果 upsert 和 multi 都为 true 并且没有文档匹配查询,更新操作只插入一个文档.
为避免多次更新插入,请确保查询字段是唯一的索引.有关示例,请参阅带有唯一索引的 Upsert.
默认为false,不匹配时不插入新文档找到了.
<小时>
更新
演示 - https://mongoplayground.net/p/iQQDyjG2a_B
使用 $function
db.collection.update({_id":sad445";},[{$set:{注意:{$函数:{正文:功能(注释){var 记录 = { 诅咒:5,块:2,得分:{ b4:40 } };if(!notes || !notes.length) { 返回 [记录];}//创建新记录并在没有笔记的情况下返回变量更新 = 假;for (var i=0; i
I need to update or create if not exist, specific obj,set score.b1 =50 and total=100 where object match curse=5 block=2
{ "_id":"sad445"
"year":2020,
"grade":4,
"seccion":"A",
"id": 100,
"name": "pedro",
"notes":[{"curse":5,
"block":1,
"score":{ "a1": 5,"a2": 10, "a3": 15},
"total" : 50
},{
"curse":5,
"block":2,
"score":{ "b1": 10,"b2": 20, "b3": 30},
"total" : 20
}
]
}
I can update all obj but I need to update or create specific elem from the score and not all. and/or create objs "notes":[{curse, block and score}] if notes is empty notes:[]
notas.UpdateMany(
{"$and":[{"_id":"sad445"},{"notes":{"$elemMatch":{"curse":5,"block":3}}}]},
{"$set":{"updated_at":{"$date":{"$numberLong":"1620322881360"}},
"notes.$.score":{"vvkzo":15,"i2z4i":2,"i2z4i|pm":5},
"notes.$.total":100}},
{"multiple":false})
Demo - https://mongoplayground.net/p/VaE28ujeOPx
Use $ (update)
The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array.
the positional $ operator acts as a placeholder for the first element that matches the query document, and
the array field must appear as part of the query document.
db.collection.update({
"notes": {
"$elemMatch": { "block": 2, "curse": 5 }
}
},
{
$set: { "notes.$.score.b4": 40 }
})
Read upsert: true
Optional. When true, update() either:
Creates a new document if no documents match the query. For more details see upsert behavior. Updates a single document that matches the query. If both upsert and multi are true and no documents match the query, the update operation inserts only a single document.
To avoid multiple upserts, ensure that the query field(s) are uniquely indexed. See Upsert with Unique Index for an example.
Defaults to false, which does not insert a new document when no match is found.
Update
Demo - https://mongoplayground.net/p/iQQDyjG2a_B
Use $function
db.collection.update(
{ "_id": "sad445" },
[
{
$set: {
notes: {
$function: {
body: function(notes) {
var record = { curse:5, block:2, score:{ b4:40 } };
if(!notes || !notes.length) { return [record]; } // create new record and return in case of no notes
var updated = false;
for (var i=0; i < notes.length; i++) {
if (notes[i].block == 2 && notes[i].curse == 5) { // check condition for update
updated = true;
notes[i].score.b4=40; break; // update here
}
}
if (!updated) notes.push(record); // if no update push the record in notes array
return notes;
},
args: [
"$notes"
],
lang: "js"
}
}
}
}
]
)
这篇关于从嵌套文档数组 mongodb 更新特定元素,其中有两个匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!