1

I have the following mongodb collection:

Channels [
{
  _id:'1'
  members:[
  {
    _id:'1'
    skipRounds:1
  },
  {
    _id:'2'
    skipRounds:3
  },       
  ]
},
{
  _id:'2'
  members:[
  {
    _id:'1'
    skipRounds:3
  },
  {
    _id:'2'
    skipRounds:5
  },       
  ]
},   
]

I am trying to be able to set the value on skipRounds for a specific user in a specific channel.

For example: I've tried to set skipRounds for the user with id 2 in channel with id 1 to 10

db.Channels.updateOne({
    '_id':'1',
    'members._id' : '2'
}, {
  $set: {'users.$.skipRounds': 10}
})

but it doesn't seem to work. Anyone knows why?

4

2 回答 2

0

替换 $set: {'users.$.skipRounds': 10}$set: {"members.skipRounds": 10}示例

db.Channels.updateOne({
    "_id":"1",
    "members._id" : "2"
}, {
  $set: {"members.skipRounds": 10}
})
于 2019-10-16T13:22:39.897 回答
0

要更新嵌套数组中的元素,请尝试以下操作:

db.Channels.update(
    {"_id" : "1", "members._id" : "2"},
    {
        $set : {"members.$.skipRounds" : 10}
    })
于 2019-10-16T12:16:32.797 回答