Update an item in an array that is in an array

后端 未结 1 479
一个人的身影
一个人的身影 2020-12-06 03:04

I have a document in a mongodb collection like this :

{
    sessions : [
        {
            issues : [ 
                {
                    id : \"6e184         


        
相关标签:
1条回答
  • 2020-12-06 03:35

    You have to use this (apparently equivalent) query:

    db.mycollection.update({"sessions.0.issues": {$elemMatch: {id: <yourValue>}}}, {$set: {"sessions.0.issues.$.text": "newText"}})
    

    Notice that your update expression was correct.

    More information about $elemMatch.

    Btw, MongoDB reference explicits that $ operator does not work "with queries that traverse nested arrays".

    Important: $elemMatch only works with version 4 or more.

    0 讨论(0)
提交回复
热议问题