I have a sample document:
{
_id: 1,
exams: [
{name: 'a', score: 2},
{name: 'b', score: 9},
{name: 'c', score: 7}
]
}
I need to set the score
of the first element that has a score
higher than 7 in the exams
array to 10, for a document specified by _id
.
So in the case of the sample document, I need to update it to:
{
_id: 1,
exams: [
{name: 'a', score: 2},
{name: 'b', score: 10},
{name: 'c', score: 7}
]
}
What would the Mongo shell query be for this task?
Use $gt to find higher score
updateOne({"exams.score":{ $gt: 7 }},{$set:{"exams.$.score":"10"})
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加