我明白了为什么我们不能更新主键,因为主键被索引为唯一。但是,我找不到为什么不能更新地理位置数据,该数据被索引为2dsphere。我正在尝试更新集合中的地理位置数据,但除非我从查询的$set中删除位置部分,否则不会发生更新。是这样的还是我做错了什么?如果有另一种方法可以这样做,请帮助。谢谢。
这是我的索引
{ key: { phone: 1 }, unique: true },
{ key: { slug: 1 } },
{ key: { location: "2dsphere" } },
{ key: { name: "text" } }发布于 2020-08-09 19:39:06
试过了然后成功了..。
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"
}https://stackoverflow.com/questions/63330490
复制相似问题