near and max_distance request - mongo 2.6
Since I updated mongodb from 2.4 to 2.6, my near geo request doens't work.. From my models, I have :
class Note(Document):
location = PointField()
Then I try to get all notes arround a point
notes = Note.objects(location__near=location, location__max_distance=100)
I get this error
OperationFailure: database error: Can't canonicalize query: BadValue geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 50
I tried reproducing it with:
class Note(Document):
location = PointField()
n = Note(location=[5.0, 5.0])
n.save()
notes = Note.objects(location__near=[5.0, 5.0], location__max_distance=100)
assert notes.count() == 1
But it worked just fine @dridk, Could you please provide a scenario that reproduces it? BTW, its weird that in your query the max distance in 100, and in the error it is 50...
When working with version 2.4.8 this works for me however with version 2.6.5 I get this same OperationFailure error.
fences = Fence.objects(
mongodb.Q(center__near=[lon, lat], center__max_distance=radius)
& mongodb.Q(status__nin=['inactive'])
).limit(limit)
Right now the only work around I have identified is using a raw query:
fences = Fence.objects(__raw__= {
'center': {
'$near': {
'$geometry': {
'type': "Point",
'coordinates': [lon, lat]
},
'$maxDistance': radius
}
}
}).limit(limit)
Same issue under mongoengine 0.8.7 and mongodb 2.6.7.
OperationFailure: database error: Can't canonicalize query: BadValue geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 2000
Here are my model and query:
class School(Document):
loc = PointField()
School.objects.filter(loc__near=[longitude, latitude], loc__max_distance=2000)
I found that an incorrect query was issued to mongodb with $maxDistance placed in a wrong place.
{
"loc" : {
"$near" : {
"$geometry" : {
"type" : "Point",
"coordinates" : [
121,
31
]
}
},
"$maxDistance" : 2000
}
}
I am sorry but I can't seem to get the errors you are getting So I would appreciate if you could please provide a more elaborated example that leads to this error
Here is a long shot - when you upgraded from mongo 2.4.X to 2.6.X did you upgrade the location index?
The simple script below can produce this error without database upgrade. Hope this can help.
Environment
CentOS 6.6 Mongoengine 0.8.7 pymongo 2.8 MongoDB 2.6.7 Python 2.7.9
Test Script
mongobug.py
__author__ = 'microdog'
import json
from mongoengine import *
class School(Document):
loc = PointField(auto_index=True, required=True)
name = StringField(required=True)
def __str__(self):
return 'School: %s with loc %s' % (self.name, self.loc)
if __name__ == '__main__':
connect('test')
school = School()
school.loc = [121, 31]
school.name = 'School A'
school.save()
cursor = School.objects.filter(loc__near=[121.001, 31.001], loc__max_distance=2000)
print json.dumps(cursor._query, indent=1)
# This will fail
print cursor
Output
# python mongobug.py
{
"loc": {
"$near": {
"$geometry": {
"type": "Point",
"coordinates": [
121.001,
31.001
]
}
},
"$maxDistance": 2000
}
}
Traceback (most recent call last):
File "mongobug.py", line 28, in <module>
print cursor
File "/usr/local/lib/python2.7/site-packages/mongoengine/queryset/queryset.py", line 58, in __repr__
self._populate_cache()
File "/usr/local/lib/python2.7/site-packages/mongoengine/queryset/queryset.py", line 93, in _populate_cache
self._result_cache.append(self.next())
File "/usr/local/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 1137, in next
raw_doc = self._cursor.next()
File "/usr/local/lib/python2.7/site-packages/pymongo/cursor.py", line 1076, in next
if len(self.__data) or self._refresh():
File "/usr/local/lib/python2.7/site-packages/pymongo/cursor.py", line 1020, in _refresh
self.__uuid_subtype))
File "/usr/local/lib/python2.7/site-packages/pymongo/cursor.py", line 958, in __send_message
self.__compile_re)
File "/usr/local/lib/python2.7/site-packages/pymongo/helpers.py", line 113, in _unpack_response
error_object)
pymongo.errors.OperationFailure: database error: Can't canonicalize query: BadValue geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 2000
I believe your issue was fixed with #664 (currently on master branch)
I also think so. I close it for now, please reopen if still if you encounter the same problems with mongoengine 0.9+
I got the exact same issue, except I'm using legacy coordinates.
Someone please show me the syntax for placing the $maxDistance inside the $near for the following:
db.listings.find({"loc":{$near:[lng,lat],$maxDistance:0.15}});
Please 😰
@AnjirHossain I think you've passed the lng and lat parameters as string. It shd be a number. Give a try.
@AnjirHossain I had same issue and went through many many solutions but any of them didn't work as the real issue was in data.
In My Model had 2dsphere index which was correct but when using 2dshpere the location in the database should be in format of location : [ lng, lat ] which was the main issue. as mongodb does not validate location array this format.
Verify both the data in DB and points specified while querying both should be correct format.
Hope it helps someone!