Section 11.1: 创建 2dsphere 索引
使用 db.collection.createIndex() 方法创建 2dsphere 索引。2dsphere 索引的蓝图 :
db.collection.createIndex( { <location field> : "2dsphere" } )
在这里,位置字段是键,2dsphere 是索引的类型。在下面的示例中,我们将在地点集合中创建一个 2dsphre 索引。
db.places.insert(
{
loc : { type: "Point", coordinates: [ -73.97, 40.77 ] },
name: "Central Park",
category : "Parks"
})
以下操作将在地点集合的 loc 字段上创建 2dsphere 索引。
db.places.createIndex( { loc : "2dsphere" } )