0

I see this in Mongodb $push in nested array

What playlist.$.music represent.??

db.collection.update(
    { "_id": ID, "playlists._id": "58"},
    { "$push": 
        {"playlists.$.musics": 
            {
                "name": "test name",
                "duration": "4.00"
            }
        }
    }
)

my collection is like this .

"book" : [
        {
                "name" : "rupesh",
                "brand" : [
                        {
                                "name" : "classmate",
                                "price" : [
                                        {
                                                "RS" : 100,
                                                "Dollar" : 12
                                        }
                                ]
                        }
                ]
        }
]

I want to add new price details like

price : [
       {
          "RS" : 100,
           "Dollar : 12
       },
        {
            "RS" : 150,
           "Dollar : 15
        } 
      ]

please give me some suggestion

Flexo
  • 87,323
  • 22
  • 191
  • 272

1 Answers1

0

Since the $ operator is used for updating, quoting from the MongoDB documentation on $ for update,

The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array.

So in the code above, playlists.$.musics part only states to update the property music of first document that matches the criteria on the first pipeline.

I hope this helps.

Hani
  • 394
  • 4
  • 11
  • thnx for reply , but if collection is like `book : [{ brand : [{ price : { doller : 7 ,euro : 20 } }] }] ` then if we want to add `price : {doller : 8,euro : 22 } ` then ?? –  Sep 29 '17 at 18:19
  • can I use `{$push : {"book.$.brand.$.price : {dollar : 8 , euro : 22 } } } ` –  Sep 29 '17 at 18:22
  • Hi Rupesh, I don't think that will work. As what it says in the docu, _the **$** operator can not be used in queries that traverse arrays nested within other arrays_. – Hani Sep 29 '17 at 18:29
  • ,so will you please provide me some suggestion for doing this? –  Sep 29 '17 at 18:33
  • Will the brand property have a unique identifier, or will only be an array type with field **price**? – Hani Sep 29 '17 at 18:52