Mongodb正在更新一个被索引为2dsphere的字段



我明白了为什么我们不能更新索引为唯一的主键。但我找不到为什么我无法更新索引为2dsphere的地理位置数据。我正在尝试更新集合中的地理位置数据,但除非从查询的$set中删除位置部分,否则不会进行更新。这是怎么回事,还是我做错了什么?如果有其他方法可以做到这一点,请提供帮助。谢谢
这是我的索引

{ key: { phone: 1 }, unique: true },
{ key: { slug: 1 } },
{ key: { location: "2dsphere" } },
{ key: { name: "text" } }

尝试过这个,它成功了。。

db.places.insert(
{
loc : { type: "Point", coordinates: [ -73.97, 40.77 ] },
name: "Central Park",
category : "Parks"
}
)

db.places.createIndex( { loc : "2dsphere" } )

db.places.update({ "name" : "Central Park"},{
$set: { loc : { type: "Point", coordinates: [ -73.88, 43.78 ] } }
})

这会更新记录,不会造成任何问题。

db.getCollection('places').find({})

输出:

/* 1 */
{
"_id" : ObjectId("5f304f3150aad1afe92efb72"),
"loc" : {
"type" : "Point",
"coordinates" : [ 
-73.88, 
43.78
]
},
"name" : "Central Park",
"category" : "Parks"
}

最新更新