简体   繁体   中英

how to update a 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

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"

终于我找到了答案我被这个问题困扰了两天

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")}]})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM