开发者

Update data on MongoDB CLI with unwind

For the below document structure .I need to update if fare.baseFare is less than 3800 equal to 3800 in which flight.travelClass=Economy only needed to update for this condition. How can I solve it ?

{
  "source": "Chennai",
  "destination": "Kolkata",
  "flights": [
    {
      "flightId": "AI-766",
      "flightStatus": "Running",
      "airlinesName": "Air India",
      "departureTime": "11:35:00",
      "arrivalTime": "13:55:00",
      "seatsAvailable": 23,
      "fare": [
        {
          "travelClass": "Economy",
          "baseFare": 3587
        },
        {
          "travelClass": "Business",
          "baseFare": 9649
        }
      ],
      "stops": 1,
      "bookings": [
        {
          "emailId": "maria10@gmail.com",
          "bookingId": 2005,
          "bookingCost": 7174,
          "travelBookingClass": "Economy",
          "departureDate": {
            "$date": {
              "$numberLong": "1615141800000"
            }
          },
          "noOfTickets": 1,
          "ticketStatus": "Confirmed",
          "passengerDetails": [
            {
              "passengerName": "Joe",
              "passengerAge": 25,
              "gender": "Male",
              "bookingStatus": "Confirmed"
            },
            {
              "passengerName": "Jack",
              "passengerAge": 35,
              "gender": "Male",
              "bookingStatus": "Cancelled"
            }
          ]
        },
        {
          "emailId": "John94@gmail.com",
          "bookingId": 2006,
          "bookingCost": 4485,
          "noOfTickets": 1,
          "departureDate": {
            "$date": {
              "$numberLong": "-55789104600000"
            }
          },
          "travelBookingClass": "Ecomomy",
          "ticketStatus": "Confirmed",
          "passengerDetails": [
            {
              "passengerName": "Emily",
              "passengerAge": 29,
              "gender": "Female",
              "bookingStatus": "Confirmed"
            }
          ]
        }
      ]
    },
    {
      "flightId": "E6-2145",
      "flightStatus": "Cancelled",
      "airlinesName": "Indigo",
      "departureTime": "06:00:00",
      "arrivalTime": "08:50:00",
      "seatsAvailable": 10,
      "fare": [
        {
          "travelClass": "Economy",
          "baseFare": 3988
        },
        {
          "travelClass": "Business",
          "baseFare": 9999
        }
      ],
      "stops": 0,
      "bookings": []
    }
  ]
}

Tried this with the below query and it is not getting updated in the DB after executing. But I can able to see the updated changes on the CLI only

db.Flights.aggregate([
{$unwind:"$flights"},
{$match:{$and:[{"flights.fare.baseFare":{$lt:3800}},
    {"flights.fare.travelClass":{$in:["Economy"]}}]
}},
{$unwind:"$flig开发者_如何学Gohts.fare"},
{$match:{"flights.fare.travelClass":{$in:["Economy"]}}},
{$set:{"flights.fare.baseFare":{$max:3800}}},
{$project:{_id:0,"flights.airlinesName":1,"flights.fare":1}}
]
).pretty()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜