在数组字段上创建索引

您可以在包含数组值的字段上创建索引,以提高对该字段的查询性能。当您在包含数组值的字段上创建索引时,MongoDB 将该索引存储为多键索引。

要创建索引,请使用该db.collection.createIndex() 方法。您的操作应该类似于此原型:

db.<collection>.createIndex( { <field>: <sortOrder> } )

关于此任务

本页上的示例使用students包含以下文档的集合:

db.students.insertMany( [
   {
      "name": "Andre Robinson",
      "test_scores": [ 88, 97 ]
   },
   {
      "name": "Wei Zhang",
      "test_scores": [ 62, 73 ]
   },
   {
      "name": "Jacob Meyer",
      "test_scores": [ 92, 89 ]
   }
] )

您定期运行一个查询,返回至少大于 1 的 test_score学生90。您可以在字段上创建索引 test_scores以提高此查询的性能。

步骤

以下操作在 集合test_scores的字段上创建升序多键索引students

db.students.createIndex( { test_scores: 1 } )

由于test_scores包含数组值,MongoDB 将此索引存储为多键索引。

结果

该索引包含字段中出现的每个单独值的键 test_scores。索引是升序的,这意味着键按以下顺序存储:[ 62, 73, 88, 89, 92, 97 ]

索引支持对字段进行选择的查询test_scores。例如,以下查询返回数组中至少有一个元素test_scores大于 90 的文档:

db.students.find(
   {
      test_scores: { $elemMatch: { $gt: 90 } }
   }
)

输出:

[
   {
      _id: ObjectId("632240a20646eaee87a56a80"),
      name: 'Andre Robinson',
      test_scores: [ 88, 97 ]
   },
   {
      _id: ObjectId("632240a20646eaee87a56a82"),
      name: 'Jacob Meyer',
      test_scores: [ 92, 89 ]
   }
]

了解更多

Copyright © 上海锦木信息技术有限公司 all right reserved,powered by Gitbook文件修订时间: 2023-09-01 17:10:26

results matching ""

    No results matching ""