Home > Back-end >  how to update a document use $ operator in mongodb
how to update a document use $ operator in mongodb

Time:06-23

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

Full data link ---> link

I tried below code but it's modifying entire seat_staus

db.seats.updateOne({"show_seats.showByDate.shows.showSeats._id" :ObjectId("62b16d9ff48ce9a5a15a79d3")}, {$set:{'show_seats.$[].showByDate.shows.$[].showSeats.$[].seat_status': true }})

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"
                    },
                    "seat_number": "2",
                    "tag_name": "A",
                    "seat_status": false,
                    "user_id": false,
                    "price": "110",
                    "seats_category": "CLASSIC",
                    "show_time": "2022-06-20T10:00"

CodePudding user response:

finally I found the answer It's been two day that I stuck with this problem

db.seats.updateOne({"show_seats.showByDate.shows.showSeats._id" :ObjectId("62b16d9ff48ce9a5a15a79d6")},{$set:{"show_seats.$[].showByDate.shows.$[].showSeats.$[elem].seat_status":"my name is hrithik"}},{arrayFilters:[{"elem._id":ObjectId("62b16d9ff48ce9a5a15a79d6")}]})
  • Related