Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.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 嵌入式数组上的Mongo索引和使用$lt和$gt的查询_Mongodb_Indexing - Fatal编程技术网

Mongodb 嵌入式数组上的Mongo索引和使用$lt和$gt的查询

Mongodb 嵌入式数组上的Mongo索引和使用$lt和$gt的查询,mongodb,indexing,Mongodb,Indexing,我有一个文档集,看起来像这样: {'data': [{'Depth': 0.0, 'Value': 123.0}, {'Depth': 0.5, 'Value': 456.0}, {'Depth': 1.0, 'Value': 111.0}, {'Depth': 1.5, 'Value': 321.0}, {'Depth': 2.0, 'Value': 987.0}, {'Depth': 2.5,

我有一个文档集,看起来像这样:

{'data': [{'Depth': 0.0, 'Value': 123.0},
          {'Depth': 0.5, 'Value': 456.0},
          {'Depth': 1.0, 'Value': 111.0},
          {'Depth': 1.5, 'Value': 321.0},
          {'Depth': 2.0, 'Value': 987.0},
          {'Depth': 2.5, 'Value': 666.0},
          ...
          {'Depth': 3.0, 'Value': 453.0}],
 'datatype': 'Sometype'
curves = db.curvedata.findOne(
 {
  "datatype" : "Sometype",
  "data": {
   "$elemMatch": {
    "Depth" : { "$gt": 13000, "$lt": 13100 },
    "Value" : { "$gt": 20, "$lt": 100 } }}})
}

我希望执行如下所示的查询:

{'data': [{'Depth': 0.0, 'Value': 123.0},
          {'Depth': 0.5, 'Value': 456.0},
          {'Depth': 1.0, 'Value': 111.0},
          {'Depth': 1.5, 'Value': 321.0},
          {'Depth': 2.0, 'Value': 987.0},
          {'Depth': 2.5, 'Value': 666.0},
          ...
          {'Depth': 3.0, 'Value': 453.0}],
 'datatype': 'Sometype'
curves = db.curvedata.findOne(
 {
  "datatype" : "Sometype",
  "data": {
   "$elemMatch": {
    "Depth" : { "$gt": 13000, "$lt": 13100 },
    "Value" : { "$gt": 20, "$lt": 100 } }}})
即查找深度范围和值范围内的所有曲线


问题是-我可以索引所有的数据值,以便上面的查询是超快速的吗?如果是,那么
$lt
$gt
操作符是否会使用索引?

一个查询将只使用一个索引。 但是,您可以创建多键索引:

.ensureIndex({Depth:1,Value:1})


然后,使用
.explain()
确保查询使用的是您刚才创建的索引。

一个查询将只使用一个索引。 但是,您可以创建多键索引:

.ensureIndex({Depth:1,Value:1})


然后,使用
.explain()
确保查询使用的是您刚才创建的索引。

您当然可以!你要找的是一个多键索引。您可以通过执行以下操作来创建它

db.curvedata.ensureIndex({"data.depth": 1})

然后您可以用同样的方式进行查询。您可以在这两个查询上运行.explain(),以查找索引前和索引后节省的时间

你当然可以!你要找的是一个多键索引。您可以通过执行以下操作来创建它

db.curvedata.ensureIndex({"data.depth": 1})
然后您可以用同样的方式进行查询。您可以在这两个查询上运行.explain(),以查找索引前和索引后节省的时间

创建一个索引

.ensureIndex({'data.Depth':1,'data.Value':1})
创建一个索引

.ensureIndex({'data.Depth':1,'data.Value':1})