Replace field in mongo db database

42 views Asked by At

Helloo guys,

i have in mongo db collection:

{
    "_id" : "aghpbF6xxxCyMGg9J",
    "description" : "",
    "name" : "test",
    "roles" : [ 
        "ab.read", 
        "test.view"
    ],
    "type" : "group"
}

and i have testRoles object:

[ { name: 'testRoles',
permissions: [ 'ab.write', 'test.check' ] } ],

and basically what i need to do is, replace field roles in db with field permissions from testRoles object, please anybody can help me manage it please?

1

There are 1 answers

0
Branchverse On

You can simply $set the desired field and replace the entire array like this:

db.collection.updateOne(
{} //  empty match since idk what your match parameter is ;)
,{
    $set: {
        "roles": ['a', 'b'] // this is your value from the object
    }
})