I have below data in stored in my db. The use case is the user can choose date and update the age or name in bulk. How will be the query look like?
[
{
date:'1-1-2016',
users:[
{
'name':'james',
'age':18
},
{
'name':'alice',
'age':20
}
]
},
{
date:'2-1-2016',
users:[
{
'name':'james',
'age':18
},
{
'name':'alice',
'age':20
},
{
'name':'xiaomi',
'age':29
}
]
}
]
I used this Users.update({date:'1-1-2016','user.name':'james'},{'$set':'users.$.age':5}})
but it's not bulk update, it only update single document.
You can set multi true option in mongodb update
ex. if you want to update multiple document use
Users.update({date:'1-1-2016','user.name':'james'},{'$set':'users.$.age':5}}, {multi:true})
but if there is many dates to match use
Users.update({date:{ $in: [ '1-1-2016','1-2-2016' ] },'user.name':'james'},{'$set':'users.$.age':5}}, {multi:true})
You can use updateMany
. It is a special function for bulk update. Please note update
is deprecated .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.