使用$ not的MongoDB地理空间查询

时间:2011-11-14 18:07:05

标签: mongodb geospatial

我有一个基本的地理空间查询在MongoDB中运行良好。似乎应该很容易应用$来获得补充...但它不适合我。这是简单的用户错误吗?或者MongoDB可以在概念上不处理此查询?我在documentation中找不到任何此类限制。

工作查询(正确查找距离中心10英里范围内的2个城市):

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

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

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
}

对于想要复制/粘贴查询以查看错误的任何人,这里只是一小部分示例数据:

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。)

1 个答案:

答案 0 :(得分:5)

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

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

索引编制的问题在于,通常,否定是 EVIL 。当你转动它们时,大多数索引都不能很好地应对,所以即使你的查询有效,它也可能不合适,因为它可能不得不进行表扫描。

我对此并不完全确定,向新闻组发送消息可能是一个好主意。