我的收藏集中有以下文件。
{
"_id" : ObjectId("5c98760c27b9fb1266f996b3"),
"user_id" : ObjectId("5c8109785b9a5427b167542a"),
"permissions" : [
{
"dashboard" : [
"Pie Chart",
"Bar Chart"
],
"product management" : [
"update",
"delete",
"list",
"insert"
],
"app user listing" : [
"insert",
"update",
"delete",
"list"
]
}
]
}
我正在尝试执行以下查询:
db.user_permissions.update(
{ user_id: ObjectId("5c8740860c5dcf6c615c1ba3") },
{
"permissions.app user listing": ['inserts']
}
)
我要做的是更新“应用程序用户列表”的值。
任何帮助表示赞赏。
编辑: 这就是我想要的
预期产出:
{
"_id" : ObjectId("5c98760c27b9fb1266f996b3"),
"user_id" : ObjectId("5c8109785b9a5427b167542a"),
"permissions" : [
{
"dashboard" : [
"Pie Chart",
"Bar Chart"
],
"product management" : [
"update",
"delete",
"list",
"insert"
],
"app user listing" : [
"inserts"
]
}
]
}
我想用数组更新给定键的值。
答案 0 :(得分:1)
这适用于mongo的所有版本,您需要第二个参数进行查找,
db.myColl.update(
{
"user_id" : ObjectId("5c8109785b9a5427b167542a"),"permissions.app user listing":["insersssts"]
},
{
$set: {
"permissions.$.app user listing":["insertss"]
}
});
答案 1 :(得分:-1)
尝试如下:
db.user_permission.update(
{
"user_id" : ObjectId("5c8109785b9a5427b167542a")
},
{
$set: {
"permissions.$[].app user listing": ['inserts']
}
},
{
upsert:false
}
)
运行查询之前的数据:
{
"_id" : ObjectId("5c98760c27b9fb1266f996b3"),
"user_id" : ObjectId("5c8109785b9a5427b167542a"),
"permissions" : [
{
"dashboard" : [
"Pie Chart",
"Bar Chart"
],
"product management" : [
"update",
"delete",
"list",
"insert"
],
"app user listing" : [
"insert",
"update",
"delete",
"list"
]
}
]
}
运行查询后:
{
"_id" : ObjectId("5c98760c27b9fb1266f996b3"),
"user_id" : ObjectId("5c8109785b9a5427b167542a"),
"permissions" : [
{
"dashboard" : [
"Pie Chart",
"Bar Chart"
],
"product management" : [
"update",
"delete",
"list",
"insert"
],
"app user listing" : [
"inserts"
]
}
]
}