首先使用简单的密钥进行地理空间索引 [英] Geospatial Indexing with a simple key first

查看:91
本文介绍了首先使用简单的密钥进行地理空间索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读了有关MongoDB和地理空间索引

After reading about MongoDB and Geospatial Indexing

令我惊讶的是它不支持不是以2d索引开头的复合键.

I was amazed that it did not support compound keys not starting with the 2d index.

我不知道我是否可以从中获得任何收益,但是现在,mssql解决方案还是一样的慢/快.

I dont know if I would gain anything on it, but right now the mssql solution is just as slow/fast.

SELECT TOP 30 * FROM Villages WHERE SID = 10 ORDER BY (math to calc radius from the center point)

这行得通,但是很慢,因为它不够智能,无法使用索引,因此必须计算具有该SID的所有村庄的半径.

This works, but is slow because it not smart enough to use a index so it has to calc the radius for all villages with that SID.

所以在Mongo中,我想创建一个像这样的索引:{sid: 1, loc: "2d"},这样我就可以从一开始就过滤掉很多东西.

So in Mongo I wanted to create an index like: {sid: 1, loc: "2d"} so I could filter out alot from the start.

我不确定对此有任何解决方案.我考虑过为每个sid创建一个集合,因为它们不共享任何信息.但是,这样做的缺点是什么?还是这是人们的方式?

I'm not sure there are any solutions for this. I thought about creating a collection for each sid since they don't share any information. But what are the disadvantages of this? Or is this how people do it ?

更新

地图是平坦的:800、800到-800,-800,村庄是从地图中心到外面的地方.大约有300个不相关的不同地图,因此它们可能位于diff集合中,但不确定开销.

The maps are flat: 800, 800 to -800,-800, villages are places from the center of the map and out. There are about 300 different maps which are not related, so they could be in diff collections, but not sure about the overhead.

如果需要更多信息,请告诉我.

If more information is need, please let me know.

我尝试过的事情

> var res = db.Villages.find({sid: 464})
> db.Villages.find({loc: {$near: [50, 50]}, sid: {$in: res}})
error: { "$err" : "invalid query", "code" : 12580 }
>

也尝试过

db.Villages.find({loc: {$near: [50, 50]}, sid: {$in: db.Villages.find({sid: 464}, {sid: 1})}})
error: { "$err" : "invalid query", "code" : 12580 }

我不太确定自己在做什么错,但可能与语法有关.这里很困惑.

I'm not really sure what I'm doing wrong, but its probably somthing about the syntax. Confused here.

推荐答案

您已经说过,Mongodb无法接受位置作为地理索引中的辅助键. 2d必须排在索引的首位.因此,您在这里无法更改索引方式.

As you stated already Mongodb cannot accept location as secondary key in geo index. 2d has to be first in index. So you are out of luck here in changing indexing patterns here.

但是有一种解决方法,您可以在sid上创建两个单独的索引,而在loc和sid上创建一个复合索引,而不是复合地理索引

But there is a workaround, instead the compound geo index you can create two separate indexes on sid and one compound index with loc and sid

  db.your_collection.ensureIndex({sid : 1})
  db.your_collection.ensureIndex({loc : '2d',sid:1})

或sid和loc上的两个单独索引

or two separate indexes on sid and loc

  db.your_collection.ensureIndex({sid : 1})
  db.your_collection.ensureIndex({loc : '2d'})

(不确定上述哪一项有效,您可以自己尝试)

(am not sure which of the above one is efficient, you can try it yourself)

您可以进行两个不同的查询,以首先按sid进行过滤,然后按位置进行过滤,类似这样

and you can make two different queries to get the results filterd by sid first and the location next, kinda like this

  res = db.your_collection.find({sid:10})
  //get all the ids from the res (res_ids)
  //and query by location using the ids
  db.your_collection.find({loc:{ $near : [50,50] } ,sid : {$in : res_ids}})  

这篇关于首先使用简单的密钥进行地理空间索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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