首页 > 解决方案 > 构建 MongoDB 索引以覆盖所有类型的查询的最佳方法

问题描述

我正在构建一个查询引擎,其底层数据库是 DocumentDB(又名 MongoDB 3.6),我想知道为它构建索引的最佳方法是什么,以获得最佳的查询性能。虽然肯定会有某些查询比其他查询更常见,但事实上用户将构建这些查询,目标是在查询的任何属性组合中具有良好(足够)的性能。

此集合中的文档将具有类似于以下的结构:

{ "ContainerName" : "hello",
  "description" : "test",
  "timestamp" : 1000000,
  "isActive" : true,
  (15 more attributes of strings, booleans, numbers),
  "events": [
      { "eventId" : "test",
        (10 more attributes of strings, booleans, numbers)
      },
      { "eventId" : "test2",
        (10 more attributes of strings, booleans, numbers)
      }],
  "resources": [
      { "resourceId" : "test",
        (8 more attributes of strings, booleans, numbers)
      }]
}

我希望能够查询所有属性组合,包括嵌入式属性。例如,给我一个名为 hello 的容器,它有一个带有 test 的 eventId 的事件。如果属性的数量足够少,也许我可以将所有可能的组合作为复合键,但在这里不可能。

此外,如果我想在某些字段上对字符串包含使用正则表达式,MongoDB 是否会在它可以使用的过滤器属性上使用索引,然后手动过滤以完成正则表达式,或者使用正则表达式完全消除对查询的索引的使用。

标签: mongodbaws-documentdb

解决方案


推荐阅读