当前位置: 动力学知识库 > 问答 > 编程问答 >

mongodb - Updating embedded mongo array values

问题描述:

I currently have a mongo object set up like so.

{

"_id" : "FtFKS23swBSEtEJAK",

"active_users" : [

{

"user" : {

"profile" : {

...

}

},

"maxMessages" : 25

},

{

"user" : {

"profile" : {

...

}

}

"maxMessages" : 25

}

],

"display_name" : "Testing"

}

Now, I am trying to run a mongo query that will go through the embedded active users and increment the maxMessages by a given value.

I cant seem to get it right though.

This is my failed attempt

activeUsersLength = Rooms.findOne({}).active_users.length

for i in [0..activeUsersLength-1]

Rooms.update({_id: room}, {$inc: {'active_users.' + i + '.maxMessages': 1}})

网友答案:
Rooms.update({_id:room, "active_users.user": { $exists : true } },{$inc:{"active_users.$.maxMessages":1}  })

I have not tested

Take a look http://docs.mongodb.org/manual/reference/operator/projection/positional/#proj.S

You can t update more element in Array https://jira.mongodb.org/browse/SERVER-1243 thanks to @dgiugg's comment

分享给朋友:
您可能感兴趣的文章:
随机阅读: