我有一個名為權限的欄位,它是一個權限陣列,與我的應用程式的特定部分有關,每個部分都有內部更新、創建、洗掉和更新的布林值。
我想用默認值推送一個新的“部分”,但有些出錯了。這是查詢
db.getCollection('users').updateOne({ nick_name: 'a.siurob' },
{
$push: {
'permissions': {
'account_control': {
'update': true,
'read': true,
'create': true,
'delete': true
}
}
}
});
它回傳
The field 'permissions' must be an array but is of type object in document {_id: ObjectId('618d6aeb93b594490ea6c2eb')}
這是權限值的當前結構
permissions: {
"dashboard": {
"read": false,
"create": false,
"update": false,
"delete": false
},
"meals": {
"read": true,
"create": true,
"update": true,
"delete": true
},
"ingredients": {
"read": true,
"create": true,
"update": true,
"delete": true
},
"users": {
"read": true,
"create": true,
"update": true,
"delete": true
},
}
我的代碼有什么問題?
uj5u.com熱心網友回復:
詢問
- push 適用于陣列,但您有嵌套檔案
- 我認為您只需要
$set使用 path(. syntax) 在嵌套檔案中添加欄位
玩蒙哥
db.users.updateOne(
{"nick_name": {"$eq": "a.siurob"}},
{"$set":
{"permissions.account_control":
{"update": true, "read": true, "create": true, "delete": true}}})
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/504152.html
標籤:mongodb
下一篇:MongoDB子查詢
