如何使用django-nonrel和mongodb进行自定义查询 [英] How to make a custom query using django-nonrel and mongodb

查看:88
本文介绍了如何使用django-nonrel和mongodb进行自定义查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在使用Django nonrel对mongodb进行自定义查询的推荐方法?

Is there a recommended way to make a custom query to mongodb using django nonrel?

我已经建立了一个完整的站点,并且运行良好,现在我只是添加了一些地理空间索引和查询,并想知道是否已经为地理空间查询提供了支持,或者是否存在使用该方法的最佳实践方法定制查询?

I have an entire site set up and running well, now I am just adding in some geospatial indexing and queries, and wanted to know if for geospatial queries there is already support or if there is a best practice way to do it using a custom made query?

推荐答案

我找到了这个问题的一个答案,如果有更好的答案,让我现在.

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

如此处所述,将您的对象分配给MongoDBManager- http://django-mongodb-engine.github.com/mongodb-engine/cool-stuff.html#included-mongodb-batteries

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 MongoDBManager

class MyModel(models.Model):
    objects = MongoDBManager()

然后您可以执行以下原始查询:

Then you can do raw queries like this:

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

我猜想是直接去pymongo的另一种方法: http://api.mongodb.org/python/1.10%2B/examples /geo.html

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

这篇关于如何使用django-nonrel和mongodb进行自定义查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆