Scotty
Scotty

Reputation: 23

MongoDB embedded search

I'm trying to do a match for the 'scheduledTimeLocal' for departures only. I was using the following query:

db.schedule.find( { "departures.scheduledTimeLocal" : /2020-05-09/})

That didn't work and it also seemed too simple... So I tried using nested $elemMatch and searched for an exact match :

db.schedule.find({'departures':{$elemMatch:{$elemMatch:{$in:['2020-05-09 06:05+01:00']}}}})

But I am not getting any results with either. Below is the structure of the data in the document:

Any ideas appreciated.

    { "_id" : ObjectId("5eb5a71731da45610ce0a335"),
      "departures": [
         {
          "movement": {
            "airport": {
             "icao": "LEMG",
             "iata": "AGP",
             "name": "Málaga"
            },
            "scheduledTimeLocal": "2020-05-09 10:40+01:00",
            "scheduledTimeUtc": "2020-05-09 09:40Z",
            "quality": [
              "Basic"
            ]
          },
          "number": "FR 8008",
          "status": "Unknown",
          "codeshareStatus": "Unknown",
          "isCargo": false,
          "aircraft": {
            "model": "Boeing 737-800"
           },
          "airline": {
            "name": "Ryanair"
          }
        },
        {
          "movement": {
            "airport": {
              "icao": "EGLL",
              "iata": "LHR",
              "name": "London"
            },
            "scheduledTimeLocal": "2020-05-09 10:55+01:00",
            "scheduledTimeUtc": "2020-05-09 09:55Z",
            "quality": [
              "Basic"
            ]
          },
          "number": "BA 1307",
          "status": "Unknown",
          "codeshareStatus": "IsOperator",
          "isCargo": false,
          "aircraft": {
            "model": "Airbus A319"
          },
          "airline": {
            "name": "British Airways"
          }
        }
      ],
      "arrivals": [
        {
          "movement": {
            "airport": {
              "icao": "EIDW",
              "iata": "DUB",
              "name": "Dublin"
            },
            "scheduledTimeLocal": "2020-05-09 08:40+01:00",
            "scheduledTimeUtc": "2020-05-09 07:40Z",
            "quality": [
              "Basic"
            ]
          },
          "number": "EI 3240",
          "status": "CanceledUncertain",
          "codeshareStatus": "Unknown",
          "isCargo": false,
          "aircraft": {
            "model": "ATR 72"
          },
          "airline": {
            "name": "Aer Lingus"
          }
        }
      ]
    }

Upvotes: 2

Views: 38

Answers (1)

Kashinath Patekar
Kashinath Patekar

Reputation: 1133

You may use this query:

db.getCollection('schedule').find({
     'departures.movement.scheduledTimeLocal' : "2020-05-09 10:40+01:00"
})

Upvotes: 1

Related Questions