I have the following collection
users
{
"_id" : ObjectId("..."),
"username" : "admin",
"permissions" : {
"keycloakId" : "...",
"roles" : [
"admin"
]
},
}
I need to update roles field. I want to assign new role to every user. How can I iterate over collection using mongo script which I am gonna put to docker-entrypoint-initdb.d / init.js?
Here is my code
db.getCollection('users').find({}).forEach(
function (elem) {
elem.update({
_id: elem._id
}, {
$set: {
permissions.roles: ['newRole']
}
}
);
}
)
I got this error:
2022-12-01T09:51:17.394+0000 E QUERY [js] uncaught exception: SyntaxError: missing : after property id :
@(shell):9:30
2022-12-01T09:51:17.395+0000 E QUERY [js] uncaught exception: SyntaxError: expected expression, got '}' :