Jaroslav Dušek
Jaroslav Dušek

Reputation: 1

Replace field in mongo db database

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?

Upvotes: 0

Views: 41

Answers (1)

Branchverse
Branchverse

Reputation: 1397

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
    }
})

Upvotes: 1

Related Questions