Can't update specific element of nested array inside collection

Hi all,
I’m having strange problem with updateOne, probably because I’m fresh on MongoDB and I’m doing some rookie error :slight_smile:
My collection looks like this:

_id: ObjectId(“628b3de8b94528801b9c50d0”),
name: ‘Piccolo’,
theatre: ‘Cinestar’,
rows: 2,
columns: 2,
seats: [
{ row: 0, col: 0, type: ‘X’ },
{ row: 1, col: 0, type: ‘X’ },
{ row: 0, col: 1, type: ‘X’ },
{ row: 1, col: 1, type: ‘X’ }

I want to be able to update property Type for every element inside seats array so I use this:

db.auditoriums.updateOne({_id: ObjectId(“628b3de8b94528801b9c50d0”), “seats.row”: 1, “seats.col”: 0},{$set: {“seats.$.type”: “Z”}})

and this works every time I change only row attribute. But if I change col attribute too it doesn’t match and doesn’t do any change.

acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 0,
upsertedCount: 0

What I’m doing wrong?

Hello @Misko_Misic, welcome to the MongoDB community forum!

You can find the solution in the topic of the manual, Update Embedded Documents Using Multiple Field Matches, linked below. When matching multiple fields in the array of sub-documents you need to use the $elemMatch operator.

Hello @Prasad_Saya
thank you for your answer, I solved my problem.


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.