问题描述
如果不存在,我需要更新或创建特定对象,设置 score.b1 =50 和 total=100,其中对象匹配 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
}
]
}
我可以更新所有 obj,但我需要更新或从分数中创建特定元素,而不是全部。和/或创建 objs "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}},{"multiple":false})
解决方法
演示 - https://mongoplayground.net/p/VaE28ujeOPx
位置 $ 运算符标识数组中要更新的元素,而无需明确指定该元素在数组中的位置。
位置 $ 运算符充当与查询文档匹配的第一个元素的占位符,并且
数组字段必须作为查询文档的一部分出现。
db.collection.update({
"notes": {
"$elemMatch": { "block": 2,"curse": 5 }
}
},{
$set: { "notes.$.score.b4": 40 }
})
读取upsert:真
可选。当为真时,update() 要么:
如果没有文档与查询匹配,则创建一个新文档。更多 详细信息请参见 upsert 行为。更新匹配的单个文档 查询。如果 upsert 和 multi 都为 true 并且没有文档匹配 查询,更新操作只插入一个文档。
为避免多次更新插入,请确保查询字段是唯一的 索引。有关示例,请参阅带有唯一索引的 Upsert。
默认为false,不匹配时不插入新文档 找到了。
更新
演示 - https://mongoplayground.net/p/iQQDyjG2a_B
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"
}
}
}
}
]
)
,
尝试添加 upsert: true
。
如果没有文档与查询匹配,则创建一个新文档。更新匹配的单个文档 查询。
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,"upsert":true})