Query by distance and date with Pymongo [duplicate]

I know this is a very basic question, but I don’t understand how to do this.

I’m using pymongo 3.6.1 in Python 3 and MongoDB 3.4.19. I’m trying to retrieve from the database the records lower than certain date, sorted by distance. By separate the functions are:

db[database].MIDAS_stations.find(
        {
            "loc": {
                "$near": {
                  "$geometry": {
                    "type": "Point",
                    "coordinates": coords
                  }
                }
              }
        }
    )

and

db[database].MIDAS_stations.find(
        {
          "Station start date": {
            "$gt": date1
          }
        }
    )

I have tried with aggregate instead of find, but I still haven’t found the way to do this.

I know that there’s something similar answered at MongoDB/PyMongo: Querying multiple criteria - unexpected results, but my question is different as it makes use of georeferenced data.

From MongoDB/PyMongo: Querying multiple criteria - unexpected results and https://stackoverflow.com/a/23577413/2313887 I think that the answer to this problem is:

db[database].MIDAS_stations.find(
    {            
        "loc": {
            "$near": {
                "$geometry": {
                    "type": "Point",
                    "coordinates": coords
                }
            }
        },
        "Station start date": {
                "$lt": date1
            }
    }
)

But I’m still uncertain if this is the right approach or if it is really querying for the nearest entries and then selecting only those with a date lower than date1.

#jquery #mongodb

1 Likes1.90 GEEK