How to update a nested document use $ operator in mongodb

I’m trying to update a document, this is my code I’m new to the mongodb I’m trying to update a nested array of object this is my query please tell me what is wrong with this query

db.seats.updateOne({
   "show_seats.$.showByDate.shows.$.showSeats.$._id":"new ObjectId(""62b0d1a72f155a7ad94cc831"")"
},
{
   "$set":{
      "show_seats.$[].showByDate.shows.$[].showSeats.$[].seat_status":false
   }
})

and this is my data look like

{
    "_id": {
        "$oid": "62b0c3342f155a7ad94cc81c"
    },
    "totalShowByDay": "2",
    "totalShowDays": 4,
    "movieId": {
        "$oid": "62b04c782828dd04f0d1c1ad"
    },
    "screenId": {
        "$oid": "62b04b8e2828dd04f0d1c1ac"
    },
    "createdAt": 1655751476553,
    "showId": {
        "$oid": "62b0c3342f155a7ad94cc6db"
    },
    "show_seats": [{
        "showByDate": {
            "ShowDate": "2022-06-20",
            "shows": [{
                "showTime": "2022-06-20T10:00",
                "showSeats": [{
                    "_id": {
                        "$oid": "62b0c3342f155a7ad94cc6dc"
                    },
                    "seat_number": "1",
                    "tag_name": "A",
                    "seat_status": false,
                    "user_id": false,
                    "price": "110",
                    "seats_category": "CLASSIC",
                    "show_time": "2022-06-20T10:00"
                }, {
                    "_id": {
                        "$oid": "62b0c3342f155a7ad94cc6dd"

The $ does not belong in the query parameter. Only in the update parameter.

Verify your syntax and check examples.

Hi @Abhijith_Vikraman_pillai - Welcome to the community.

In addition to what steevej has advised, the data / document you provided appears to be incomplete / invalid. The query parameter ObjectId value doesn’t appear to exist in the sample document as well.

However, in saying so, I presume you are trying to update the seat_status value from false to true based off my interpretation of your update command. Please take a look at the below example query in which I was able to update the seat_status value on my test environment:

db.seats.updateOne(
{
  'show_seats.showByDate.shows.showSeats._id': ObjectId("62b0c3342f155a7ad94cc6dc")
},
{
  '$set': {
    'show_seats.$[e1].showByDate.shows.$[e2].showSeats.$[e3].seat_status': true
  }
},
{
  arrayFilters: [
    { 'e1.showByDate.shows': { '$exists': true } },
    { 'e2.showSeats': { '$exists': true } },
    { 'e3.seat_status': false }
  ]
}
)

Document after the update:

{
   "_id":"62b0c3342f155a7ad94cc81c",
   "totalShowByDay":"2",
   "totalShowDays":4,
   "movieId":"62b04c782828dd04f0d1c1ad",
   "screenId":"62b04b8e2828dd04f0d1c1ac",
   "createdAt":1655751476553,
   "showId":"62b0c3342f155a7ad94cc6db",
   "show_seats":[
      {
         "showByDate":{
            "ShowDate":"2022-06-20",
            "shows":[
               {
                  "showTime":"2022-06-20T10:00",
                  "showSeats":[
                     {
                        "_id":"62b0c3342f155a7ad94cc6dc",
                        "seat_number":"1",
                        "tag_name":"A",
                        "seat_status":true, /// `seat_status` now true
                        "user_id":false,
                        "price":"110",
                        "seats_category":"CLASSIC",
                        "show_time":"2022-06-20T10:00"
                     },
                     {
                        "_id":"62b0c3342f155a7ad94cc6dd"
                     }
                  ]
               }
            ]
         }
      }
   ]
}

You would need to edit the arrayFilters accordingly for your use case.

Please note, this was not thoroughly tested and it is highly recommended to test extensively within a test environment before running any commands in production.

I would also recommend going over the $[<identifier>] documentation to learn more about the update example above. Additionally, If this is a common operation in the database, the following pages may be of use to you regarding data modelling:

If you require further assistance with this, could you provide the following information:

  1. Complete sample document(s)
  2. The expected output

Hope this helps.

Regards,
Jason

2 Likes