【发布时间】:2015-02-28 08:16:46
【问题描述】:
我正在分析文本。这些文本有注释(例如“章节”、“风景”……)。这些注释在我的 MongoDB 集合annotations 中,例如
{
start: 1,
stop: 10000,
type: chapter,
details: {
number: 1,
title: "Where it all began"
}
},
{
start: 10001,
stop: 20000,
type: chapter,
details: {
number: 2,
title: "Lovers"
}
},
{
start: 1,
stop: 5000,
type: scenery,
details: {
descr: "castle"
}
},
{
start: 5001,
stop: 15000,
type: scenery,
details: {
descr: "forest"
}
}
挑战 1:对于文本中的给定位置,我想查找所有注释。例如查询字符1234 应该告诉我,那个
- 在第一章内
- 故事发生在城堡里
挑战 2:我也喜欢查询范围。例如查询9800 to 10101 形式的字符应该告诉我,它涉及chapter 1、chapter 2 和scenery forest。
挑战 3:类似于 挑战 2 我只想匹配那些完全被查询范围覆盖的注释。例如查询9800 to 30000形式的字符应该只返回文档chapter 2。
对于挑战 1,我尝试简单地使用 $lt 和 $gt。例如:
db.annotations.find({start: {$lt: 1234}, stop: {$gt: 1234}});
但我意识到,即使我有 start 和 stop 的复合索引,也只使用键 start 的索引。有没有办法为我提到的三个问题创建更充分的索引?
我很快想到了地理空间索引,但我还没有使用它们。我也只需要它的一维版本。
【问题讨论】:
标签: mongodb indexing geospatial compound-index