创建二维索引

二维索引支持对平面欧几里得平面中的位置数据进行查询。

要创建二维索引,请使用该db.collection.createIndex() 方法。索引类型是"2d"

db.<collection>.createIndex( { <location field> : "2d" } )

关于此任务

  • 中的值<location field>必须是旧坐标对。
  • 指定旧坐标对时,首先列出经度,然后列出纬度
    • 有效的经度值介于-180和之间180(包含两者)。
    • 有效的纬度值介于-90和之间90(包含两者)。

在你开始之前

创建contacts集合:

db.contacts.insertMany( [
   {
      name: "Evander Otylia",
      phone: "202-555-0193",
      address: [ 55.5, 42.3 ]
   },
   {
      name: "Georgine Lestaw",
      phone: "714-555-0107",
      address: [ -74, 44.74 ]
   }
] )

address字段包含旧坐标对。

过程

在字段上创建二维索引address

db.contacts.createIndex( { address : "2d" } )

下一步

创建二维索引后,您可以使用二维索引来支持位置数据的计算。要查看使用二维索引的查询示例,请参阅:

了解更多

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

results matching ""

    No results matching ""