How to make a custom query using django-nonrel and mongodb How to make a custom query using django-nonrel and mongodb mongodb mongodb

How to make a custom query using django-nonrel and mongodb


I found one answer to this question, let me now if there is a better one.

As documented here assign your objects to the MongoDBManager - http://django-mongodb-engine.github.com/mongodb-engine/cool-stuff.html#included-mongodb-batteries

from django_mongodb_engine.contrib import MongoDBManagerclass MyModel(models.Model):    objects = MongoDBManager()

Then you can do raw queries like this:

MyModel.objects.raw_query({'loc' : {'$near' : [50,50]}})

A different approach I guess would be to go directly to pymongo:http://api.mongodb.org/python/1.10%2B/examples/geo.html

Finally I ended up with this query:

nearest = MyModel.objects.raw_query(    {'loc' : {         '$within' :{ #within .05 degrees of lat/lon                    '$center' : [{'long' : long,'lat' : lat}, .05]                    }      })[:10] #get up to 10 results