Skip to content

Unable to use $near #54

Open
Open
@martinsJoseph

Description

@martinsJoseph

Hello guys,
I am having issues with using flask-mongoalchemy to query my database and get results that are close to the longitude and latitude I passed.

My Model:

class Event(db.Document):
meta = {
'indexes': [
("*location.coordinates", "2dsphere")
]
}

user_id = db.StringField()
uuid = db.StringField()
name = db.StringField()
address = db.StringField()
start_time = db.DateTimeField(required=True, default=datetime.datetime.now())
end_time = db.DateTimeField(required=True, default=datetime.datetime.now())
location = db.DictField(db.AnythingField())

When an event is created this is how the document is meant to look like:
{
"address": "Surulere",
"end_time": "2019-10-13T00:00:00",
"location": {
"coordinates": [
-119.3996898,
19.6255545
],
"type": "Point"
},
"name": "Amazonss__",
"start_time": "2019-10-12T00:00:00",
"user_id": "3b15b5f9d6d1ab93d14db52cabe6ac",
"uuid": "baa75b69047369a48342e6939c56d3"
}

And My Code for Querying is:
event = Event.query.filter({"location" :
{ "$near" :
{
"$geometry" : {
"type" : "Point",
"coordinates" : [longitude, latitude] },
"$maxDistance" : 4000
}
}
}).first()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions