problem with max_distance

224 views
Skip to first unread message

Maciej Gębarski

unread,
Aug 16, 2014, 1:04:16 PM8/16/14
to mongoeng...@googlegroups.com
Hi i have a problem with queries data using mongoengine.

I create two models

class Alarm(mongoengine.Document):
    added
= mongoengine.DateTimeField()
    title
= mongoengine.StringField()
    tracks
= mongoengine.ListField(mongoengine.EmbeddedDocumentField(Track))

    meta
= {
       
'indexes': [[("tracks.location", "2dsphere")]]
   
}

class Track(mongoengine.EmbeddedDocument):
    created_on
= mongoengine.DateTimeField()
    location
= mongoengine.PointField()




from shell i add one row :

db.alarm.insert({"title": "Warszawa", "tracks": [{"location": {"type": "Point", "coordinates": [21.01666, 52.233333]}}]})



Ok now in mongo shell i try find.

first return none second find:
>db.alarm.find({'tracks.location': {$near: {$geometry: {"type": "Point", coordinates: [18.068611, 59.329444]}, $maxDistance: 810997}}})
>


> db.alarm.find({'tracks.location': {$near: {$geometry: {"type": "Point", coordinates: [18.068611, 59.329444]}, $maxDistance: 810998}}})
{
 
"_id" : ObjectId("53ef89626dda06655a57a342"), "title" : "Warszawa",
"tracks" : [ { "location" : { "type" : "Point", "coordinates" : [
21.01666, 52.233333 ] } } ] }

And i is ok.


But when i try in django shell:
 Alarm.objects(tracks__location__near = {"coordinates":[ 21.01666, 52.233333 ] , "type": "Point"}, tracks__location__max_distance=810998)


i get:

<repr(<mongoengine.queryset.queryset.QuerySet at 0x7ff3a50e8a10>) failed: 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: 810998>

Gonzalo Infante

unread,
Aug 28, 2014, 10:18:57 AM8/28/14
to mongoeng...@googlegroups.com
Had any luck solving the problem? I'm stuck with the same issue.

Gonzalo Infante

unread,
Sep 2, 2014, 12:40:46 PM9/2/14
to mongoeng...@googlegroups.com
So far, this workaround is the only one that has worked for me. Use a raw query like this:

models.Incident.objects(__raw__={'location':{'$near':{'$geometry':{'type': "Point", 'coordinates': [lat, lon]},'$maxDistance': distance}}})



Just in case (regarding MongoEngine):
When using MongoEngine I had problems with variable naming inside the model I was trying to save. The variable containing the location was called loc, but that would throw the error you mentioned:
<repr(<mongoengine.queryset.queryset.QuerySet at 0x7ff3a50e8a10>) failed: 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: 810998>

After changing the name to anything else it would not show the error above, instead it would show another error regarding max_distance parameter:
OperationFailure: database error: Can't canonicalize query: BadValue geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 111308

This is the query in Flask:
models.Incident.objects(location__near=[lat, lon], location__max_distance=distance)

Juan Diego Restrepo

unread,
Mar 11, 2015, 12:28:58 PM3/11/15
to mongoeng...@googlegroups.com
Hi Gonzalo or anyone else, any luck with this problem?. I'm using mongoengine 0.8.7 with pymongo 2.8 and I'm getting the same error. Raw query works fine.
Reply all
Reply to author
Forward
0 new messages