0

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.

Jumy Elerossë
  • 189
  • 2
  • 3
  • 12
  • This is different to https://stackoverflow.com/a/23577250/3866828 because it's the sorting condition what is complicating my task. – Jumy Elerossë Mar 08 '19 at 02:16

1 Answers1

0

I think this is the answer:

to_check= db[database].MIDAS_stations.find(
    {
        "$and": [
            {
              "Station start date": {
                '$gt': date1
              }
            },
            {
                'loc': {
                    '$near': {
                      '$geometry': {
                        'type': "Point",
                        'coordinates': coords
                      }
                    }
                  }
            }
        ]
    }
)
Jumy Elerossë
  • 189
  • 2
  • 3
  • 12
  • 1
    Well you probably could have learned from https://stackoverflow.com/a/23577413/2313887 that the `$and` is not needed here at all. Also this has nothing to do with "sorting condition". – Neil Lunn Mar 08 '19 at 02:31
  • I just wrote that I thought it could be the answer, is not marked as such because I know that is probably wrong. I saw that link before publishing my question, knowing that ```$and``` is not needed. I guess I should use ```$sort```, but I don't know how to use with geometry types. The only thing I can find is that ```$near``` returns the results by distance. As I said in my original question, I know it's something basic, but I don't know how to do it and I spent the entire day reading yesterday trying to find the answer. – Jumy Elerossë Mar 08 '19 at 12:12