{
id: 1,
name: "venkat",
description: "Description",
address: "chennai",
pinCode: "600100",
authenticationCode: "123",
authenticated: false,
deleted: false,
Location: [
{
name: "foo",
description: "LocationDecription",
parent: "parent",
UserAccess: [
{
loginName: "paklon",
role: "role"
}
]
}
]
}
我需要更新条件匹配的role:"admin"
,例如id: 1
,Location
的数组name: "foo"
和Location
嵌套数组"UserAccess.loginName": "paklon"
和然后更新角色。
答案 0 :(得分:1)
根据上述描述作为解决方案,请尝试在MongoDB shell中执行以下更新操作
db.collection.update({
"id": 1,
"Location": {
$elemMatch: {
"name": "foo",
"UserAccess": {
$elemMatch: {
"loginName": "paklon"
}
}
}
}
}, {
$set: {
"Location.$.UserAccess.0.role": "admin"
}
})
答案 1 :(得分:0)
以下查询应该有效:
db.collection.update(
{"id": 1, "Location.name": "foo", "Location.UserAccess.loginName": "paklon"},
{$set: {"Location.0.UserAccess.0.role": "admin"}}
)
要更新数组中的特定元素,您需要使用positional operator“$”实际上是第一个元素的占位符。因此它可以替换为“0”,并且因为它允许在查询中仅使用“$”一次,所以在任何情况下都必须在查询结尾处使用“0”。
另请查看相应的mongodb documentation