Comment on page
2dsphere 索引
在本页面
- 概述
2dsphere索引版本 | 描述 |
---|---|
版本3 | MongoDB 3.2引入了一个版本3的2dsphere索引。版本3是在MongoDB 3.2和更高版本中创建的2dsphere索引的默认版本。 |
版本2 | MongoDB 2.6引入了2dsphere索引的版本2。版本2是在MongoDB 2.6和3.0系列中创建的2dsphere索引的默认版本。 |
要覆盖默认版本并指定其他版本,请在创建索引时包含选项
{“ 2dsphereIndexVersion”:<version>}
。对于包含
2dsphere
索引键和其他类型的键的复合索引,只有2dsphere
索引字段确定索引是否引用文档。MongoDB的早期版本仅支持
2dsphere (Version 1)
索引。 默认情况下,2dsphere (Version 1)
索引不是sparse索引,并且拒绝该字段为空的文档。版本2和更高版本的
2dsphere
索引包含对其他GeoJSON对象的支持:MultiPoint,MultiLineString,MultiPolygon和GeometryCollection。有关所有受支持的GeoJSON对象的详细信息,请参见GeoJSON对象。[success] 注意如果您不指定key
,您将最多只能拥有一个2dsphere
索引或一个2dsphere
索引,MongoDB首先寻找2d
索引。 如果不存在2d
索引,则MongoDB会寻找2dsphere
索引。
db.collection.createIndex( { <location field> : "2dsphere" } )
db.places.insert(
{
loc : { type: "Point", coordinates: [ -73.97, 40.77 ] },
name: "Central Park",
category : "Parks"
}
)
db.places.insert(
{
loc : { type: "Point", coordinates: [ -73.88, 40.78 ] },
name: "La Guardia Airport",
category : "Airport"
}
)
db.places.createIndex( { loc : "2dsphere" } )
复合索引可以包含
2dsphere
索引键和非地理空间索引键。例如,以下操作将创建一个复合索引,其中第一个键loc
是2dsphere
索引键,其余键category
和names
是非地理空间索引键,并分别指定降序(-1
)和升序(1
)。db.places.createIndex( { loc : "2dsphere" , category : -1, name: 1 } )
db.places.createIndex( { category : 1 , loc : "2dsphere" } )
译者:杨帅 周正
最近更新 2yr ago