Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如何查找查询值在两个键值范围内的文档_Mongodb_Indexing_Geospatial_Compound Index - Fatal编程技术网

Mongodb 如何查找查询值在两个键值范围内的文档

Mongodb 如何查找查询值在两个键值范围内的文档,mongodb,indexing,geospatial,compound-index,Mongodb,Indexing,Geospatial,Compound Index,我在分析文本。这些文本有注释(如“章节”、“风景”等)。这些注释位于我的MongoDB集合注释中,例如 { start: 1, stop: 10000, type: chapter, details: { number: 1, title: "Where it all began" } }, { start: 10001, stop: 20000, type: chapter, details: { number: 2, tit

我在分析文本。这些文本有注释(如“章节”、“风景”等)。这些注释位于我的MongoDB集合
注释中,例如

{
  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到10101
应该告诉我,它涉及
第1章
第2章
风景林

挑战3:与挑战2相比,我只想匹配那些完全被查询范围覆盖的注释。例如,查询字符表单
9800到30000
应该只返回文档
第2章

对于挑战1,我尝试简单地使用
$lt
$gt
。e、 g:

db.annotations.find({start: {$lt: 1234}, stop: {$gt: 1234}});
但我意识到,只使用键
start
的索引,即使我有一个用于
start
stop
的复合索引。有没有办法为我提到的三个问题创建更充分的索引


我很快想到了地理空间索引,但我还没有使用它们。我也只需要它的一维版本。

对于挑战1,您使用的查询是合适的,尽管您可能希望使用
$lte
$gte
来包含在内

db.annotations.find({“开始”:{“$lt”:1234},“停止”:{“$gt”:1234});
关于索引,它选择在
start
上使用索引而不是复合索引的原因与复合索引的树结构有关,Rob Moore在中很好地解释了这一点。请注意,如果您使用
hint()
,它仍然可以使用复合索引,但查询优化程序发现,在
start
上使用索引,然后删除与
stop
子句范围不匹配的结果会更快

对于挑战2,您只需要使用显式
$或
子句来涵盖
停止
在范围内、
开始
在范围内以及
开始
停止
包含范围的情况

db.annotations.find({
“$or”:[
{“停止”:{“$gte”:9800,$lte:10101},
{“开始”:{“$gte”:9800,$lte:10101},
{“开始”:{“$lt”:9800},“停止”:{“$gt”:10101}
]
});
对于挑战3,您可以使用非常类似于挑战1中的查询,但要确保文档完全被给定的边界覆盖

db.annotations.find({“开始”:{“$gte”:9800},“停止”:{“$lte”:30000});

我在你发布的链接(错误链接?)中找不到rob moore的答案。提示():mongo是否也在使用
start
stop
的信息?或者它只是使用
开始:{$lte:…}
的信息。对不起。现在修复了链接。我不知道使用
hint()。您可以尝试分析这两个查询,看看是否有任何差异。