我目前有一个带有一个文档的一个集合('locations')的Mongo DB数据库:
{
"_id" : ObjectId("5875653b89513c8328416522"),
"name" : "Starcups",
"address" : "125 High Street, Reading, RG6 1PS",
"rating" : 3,
"facilities" : [
"Hot drinks",
"Food",
"Premium wifi"
],
"coords" : [
-0.9690884,
51.455041
],
"openingTimes" : [
{
"days" : "Monday - Friday",
"opening" : "7:00am",
"closing" : "7:00pm",
"closed" : false
},
{
"days" : "Saturday",
"opening" : "8:00am",
"closing" : "5:00pm",
"closed" : false
},
{
"days" : "Sunday",
"closed" : true
}
],
"reviews" : [
{
"author" : "Simon Holmes",
"id" : ObjectId("5875663389513c8328416523"),
"rating" : 5,
"timestamp" : ISODate("2013-07-15T23:00:00Z"),
"reviewText" : "What a great place. I can't say enough good things about it."
}
]
}
我需要将字段“id”(子文档审阅的一部分)更改为“_id”。我已经尝试过,使用StackExchange上的其他类似示例,以下代码无济于事:
db.locations.update({}, {$rename:{"reviews.id":"reviews._id"}}, false, true);
但是我收到以下错误:
WriteResult({
"nMatched" : 0,
"nUpserted" : 0,
"nModified" : 0,
"writeError" : {
"code" : 16837,
"errmsg" : "cannot use the part (reviews of reviews.id) to traverse the element ({reviews: [ { author: \"Simon Holmes\", id: ObjectId('5875663389513c8328416523'), rating: 5.0, timestamp: new Date(1373929200000), reviewText: \"What a great place. I can't say enough good things about it.\" } ]})"
}
})
当我尝试更改任何其他字段时,我收到相同的错误。有人能指出我正确的方向吗?
更新
这似乎是评估子文档字段的问题,因为以下代码执行正常:
db.locations.update({}, {$rename:{"name":"names"}}, false, true);
我也尝试搜索相关文档:https://docs.mongodb.com/manual/reference/operator/update/rename/
答案 0 :(得分:0)
那是因为评论是Array
你不能$rename
这样做,而是你需要$设置新名称和$取消旧名称