I have a field called permissions, it's an array of permissions, regarding to specific section of my app, and each section has inside update, create, delete and update with a boolean value.
I want to push a new "section" with default values, but some is going wrong. This is the query
db.getCollection('users').updateOne({ nick_name: 'a.siurob' },
{
$push: {
'permissions': {
'account_control': {
'update': true,
'read': true,
'create': true,
'delete': true
}
}
}
});
And it returns
The field 'permissions' must be an array but is of type object in document {_id: ObjectId('618d6aeb93b594490ea6c2eb')}
This is the current structure of the permissions value
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
},
}
What is wrong with my code?
CodePudding user response:
Query
- push is for arrays but you have nested document
- i think you just need
$set
with path(. syntax) to add the field inside the nested document
db.users.updateOne(
{"nick_name": {"$eq": "a.siurob"}},
{"$set":
{"permissions.account_control":
{"update": true, "read": true, "create": true, "delete": true}}})