$(update)
source : http://docs.mongodb.org/manual
Update Values in an Array
Data
db.test.insert([
{ "_id" : 1, "grades" : [ 80, 85, 90, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92, 87 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90, 87 ] }
])
Update 1st value of an array from 80 to 82.
db.test.update(
{ _id : 1, grades : 80},
{ $set : { "grades.$" : 82}}
)
db.test.find({_id: 1})
Result - { "_id" : 1, "grades" : [ 82, 85, 90, 80 ] }
Update the documents that have grades value : 90.
db.test.update(
{ _id : 1, grades : 90},
{ $set : { "grades.$" : 92}},
{ multi : true}
)
Result - No effect of multi : true, only the first document gets updated.
{ "_id" : 1, "grades" : [ 82, 85, 92, 80 ] }
{ "_id" : 2, "grades" : [ 88, 90, 92, 87 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90, 87 ] }
Update Documents in an Array
Data
db.test.insert([
{ "_id" : 4, "grades" : [ { grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 90, mean: 85, std: 3 } ] },
{ "_id" : 5, "grades" : [ { grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 90, mean: 85, std: 3 } ] }
])
1. Use the positional $ operator to update the value of the std field in the embedded document with the grade of 85:
db.test.update(
{ _id: 4, "grades.grade": 85 },
{ $set: { "grades.$.std" : 6 } }
)
db.test.find({ _id: 4}).pretty()
Result
{
"_id" : 4,
"grades" : [
{
"grade" : 80,
"mean" : 75,
"std" : 8
},
{
"grade" : 85,
"mean" : 90,
"std" : 6
},
{
"grade" : 90,
"mean" : 85,
"std" : 3
}
]
}
2. update multiple docs
db.test.update(
{ "grades.grade": 90 },
{ $set: { "grades.$.std" : 4 } },
{ multi : true}
)
db.test.find().pretty()
{ "_id" : 4, "grades" : [ { grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 90, mean: 85, std: 4 } ] },
{ "_id" : 5, "grades" : [ { grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 90, mean: 85, std: 4 } ] }
source : http://docs.mongodb.org/manual
Update Values in an Array
Data
db.test.insert([
{ "_id" : 1, "grades" : [ 80, 85, 90, 80 ] },
{ "_id" : 2, "grades" : [ 88, 90, 92, 87 ] },
{ "_id" : 3, "grades" : [ 85, 100, 90, 87 ] }
])
Update 1st value of an array from 80 to 82.
db.test.update(
{ _id : 1, grades : 80},
{ $set : { "grades.$" : 82}}
)
db.test.find({_id: 1})
Result - { "_id" : 1, "grades" : [ 82, 85, 90, 80 ] }
Update the documents that have grades value : 90.
db.test.update(
{ _id : 1, grades : 90},
{ $set : { "grades.$" : 92}},
{ multi : true}
)
Result - No effect of multi : true, only the first document gets updated.
{ "_id" : 1, "grades" : [ 82, 85, 92, 80 ] }
{ "_id" : 2, "grades" : [ 88, 90, 92, 87 ] }
{ "_id" : 3, "grades" : [ 85, 100, 90, 87 ] }
Update Documents in an Array
Data
db.test.insert([
{ "_id" : 4, "grades" : [ { grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 90, mean: 85, std: 3 } ] },
{ "_id" : 5, "grades" : [ { grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 90, mean: 85, std: 3 } ] }
])
1. Use the positional $ operator to update the value of the std field in the embedded document with the grade of 85:
db.test.update(
{ _id: 4, "grades.grade": 85 },
{ $set: { "grades.$.std" : 6 } }
)
db.test.find({ _id: 4}).pretty()
Result
{
"_id" : 4,
"grades" : [
{
"grade" : 80,
"mean" : 75,
"std" : 8
},
{
"grade" : 85,
"mean" : 90,
"std" : 6
},
{
"grade" : 90,
"mean" : 85,
"std" : 3
}
]
}
2. update multiple docs
db.test.update(
{ "grades.grade": 90 },
{ $set: { "grades.$.std" : 4 } },
{ multi : true}
)
db.test.find().pretty()
{ "_id" : 4, "grades" : [ { grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 90, mean: 85, std: 4 } ] },
{ "_id" : 5, "grades" : [ { grade: 80, mean: 75, std: 8 },
{ grade: 85, mean: 90, std: 5 },
{ grade: 90, mean: 85, std: 4 } ] }