具有$ not的MongoDB地理空间查询 [英] MongoDB geospatial query with $not

查看:85
本文介绍了具有$ not的MongoDB地理空间查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的地理空间查询在MongoDB中运行良好.似乎应用$ not来获得补码应该很容易...但是它对我不起作用.这是简单的用户错误吗?还是MongoDB不能从概念上处理此查询?我在文档中找不到任何此类限制.

I have a basic geospatial query working well in MongoDB. It seems that it should be easy to apply $not to get the complement... but it's not working for me. Is it simple user error? Or can MongoDB not handle this query conceptually? I could not find any such limitation in the documentation.

正在运行的查询(正确找到距中心10英里以内的2个城市):

Working query (correctly finds the 2 cities within 10 miles of the center):

db.customers.find({ "location" : { $within : { $center : [[-117.15,32.72],0.15] } } })

尝试补码查询(所需结果是不在10英里范围内的1个城市):

Attempted complement query (desired result is the 1 city that is not within 10 miles):

db.customers.find({ "location" : { $not : { $within : { $center : [[-117.15,32.72],0.15] } } } })
error: {
    "$err" : "missing geo field (location) in : {location: { $not: { $within: { $center: [ [ -117.15, 32.72 ], 0.15 ] } } } }",
    "code" : 13042
}

对于想复制/粘贴查询以查看错误的任何人,这里有一些示例数据:

For anyone that wants to copy/paste the query to see the error, here's a tiny bit of sample data:

db.customers.ensureIndex( { "location" : "2d" } )
db.customers.save({"city":"La Mesa","state":"CA","location":[ -117.02,32.76 ]})
db.customers.save({"city":"Chula Vista","state":"CA","location":[ -117.08,32.63 ]})
db.customers.save({"city":"Mexico City","state":"Mexico","location":[-99.133244,19.4326]})

(在需要的时候,我正在使用MongoDB 1.8.2.)

(I'm using MongoDB 1.8.2 in case that matters.)

推荐答案

我认为这是不可能的.据我所知,位置查询将为您提供一个特殊的游标,该游标只能将位置查询用作参数(例如$within).

I think this is not possible. As far as I know, location queries will give you a special cursor that can only use location queries as parameters (such as $within).

v. 2.0.1给出了更具描述性的错误消息: error: { "$err" : "geo field only has 1 element", "code" : 13068 }

v. 2.0.1 gives a more descriptive error message: error: { "$err" : "geo field only has 1 element", "code" : 13068 }

索引编制的问题是,通常,否定是 EVIL .大多数索引在转过来时都无法很好地应付,因此即使您的查询有效,也可能并不理想,因为它可能必须进行表扫描.

The issue with indexing is that, in general, negation is EVIL. Most indexes don't cope well when you turn them around, so even if your query worked, it's probably not desirable because it will probably have to do a table scan.

对此我不太确定,向新闻组发送消息可能是个好主意.

I'm not entirely sure about this, a message to the newsgroup is probably a good idea.

这篇关于具有$ not的MongoDB地理空间查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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