skip to Main Content

I have a collection in mongo having nested embedded fields. How do I get operate on a field that are within another embedded field.
Here is a sample from my collection.
I have to find
Q: Display the baseFare for each class of flight E6-2135

{
        "_id" : ObjectId("64465e36056d694d9f2694ec"),
        "source" : "Bengaluru",
        "destination" : "Delhi",
        "flights" : [
                {
                        "flightId" : "I5-2878",
                        "flightStatus" : "Running",
                        "airlinesName" : "Air Asia",
                        "departureTime" : "2001-01-01 06:15",
                        "arrivalTime" : "2001-01-01 08:45",
                        "seatsAvailable" : 96,
                        "fare" : [
                                {
                                        "travelClass" : "Economy",
                                        "baseFare" : 4485
                                },
                                {
                                        "travelClass" : "Business",
                                        "baseFare" : 9485
                                }
                        ],
                        "stops" : 0,
                        "bookings" : [
                                {
                                        "emailId" : "[email protected]",
                                        "bookingId" : 2001,
                                        "bookingCost" : 13455,
                                        "travelBookingClass" : "Economy",
                                        "departureDate" : 2021,
                                        "noOfTickets" : 3,
                                        "ticketStatus" : "Confirmed",
                                        "passengerDetails" : [
                                                {
                                                        "passengerName" : "John",
                                                        "passengerAge" : 45,
                                                        "gender" : "Male",
                                                        "bookingStatus" : "Confirmed"
                                                },
                                                {
                                                        "passengerName" : "David",
                                                        "passengerAge" : 35,
                                                        "gender" : "Male",
                                                        "bookingStatus" : "Confirmed"
                                                },
                                                {
                                                        "passengerName" : "Maria",
                                                        "passengerAge" : 38,
                                                        "gender" : "Female",
                                                        "bookingStatus" : "Confirmed"
                                                }
                                        ]
                                },
                                {
                                        "emailId" : "[email protected]",
                                        "bookingId" : 2002,
                                        "bookingCost" : 4485,
                                        "departureDate" : 2021,
                                        "travelBookingClass" : "Ecomomy",
                                        "noOfTickets" : 1,
                                        "ticketStatus" : "Confirmed",
                                        "passengerDetails" : [
                                                {
                                                        "passengerName" : "Emily",
                                                        "passengerAge" : 29,
                                                        "gender" : "Female",
                                                        "bookingStatus" : "Confirmed"
                                                }
                                        ]
                                }
                        ]
                },
                {
                        "flightId" : "E6-2135",
                        "flightStatus" : "Running",
                        "airlinesName" : "Indigo",
                        "departureTime" : "2001-01-01 06:00",
                        "arrivalTime" : "2001-01-01 08:50",
                        "seatsAvailable" : 4,
                        "fare" : [
                                {
                                        "travelClass" : "Economy",
                                        "baseFare" : 3988
                                },
                                {
                                        "travelClass" : "Business",
                                        "baseFare" : 9999
                                }
                        ],
                        "stops" : 0,
                        "bookings" : [
                                {
                                        "emailId" : "[email protected]",
                                        "bookingId" : 2003,
                                        "bookingCost" : 3988,
                                        "travelBookingClass" : "Economy",
                                        "departureDate" : 2022,
                                        "noOfTickets" : 1,
                                        "ticketStatus" : "Confirmed",
                                        "passengerDetails" : [
                                                {
                                                        "passengerName" : "Steve",
                                                        "passengerAge" : 25,
                                                        "gender" : "Male",
                                                        "bookingStatus" : "Confirmed"
                                                }
                                        ]
                                },
                                {
                                        "emailId" : "[email protected]",
                                        "bookingId" : 2004,
                                        "bookingCost" : 3988,
                                        "departureDate" : 2021,
                                        "travelBookingClass" : "Economy",
                                        "noOfTickets" : 0,
                                        "ticketStatus" : "Cancelled",
                                        "passengerDetails" : [
                                                {
                                                        "passengerName" : "Richard",
                                                        "passengerAge" : 29,
                                                        "gender" : "Male",
                                                        "bookingStatus" : "Cancelled"
                                                }
                                        ]
                                }
                        ]
                }
        ]
}

I am familiar with the $unwind operator that we use to unwind embedded documents, this however returns another embedded document as a result.

2

Answers


  1. One option is to use $filter, after you $match the wanted document:

    db.collection.aggregate([
      {$match: {flights: {$elemMatch: {flightId: "E6-2135"}}}},
      {$project: {
          _id: 0,
          baseFares: {$getField: {
              input: {$first: {$filter: {
                    input: "$flights",
                    cond: {$eq: ["$$this.flightId", "E6-2135"]}
              }}},
              field: "fare"
          }}
      }}
    ])
    

    See how it works on the playground example

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search