0

Currently, I have a Mongo Collection that contains objects like this one:

{
    "_id" : "5a1d3298dece873bfd6cf593",
    "rights" : [
        ...
        {
            "module" : "messenger",
            "right" : "writeToResident",
            "default" : true
        }, 
        {
            "module" : "messenger",
            "right" : "writeToUnionCouncil",
            "default" : true
        },
        {
            "module" : "news",
            "right" : "see",
            "default" : true
        },
        ...
    ]
}

I just want to do something simple: edit the default value in the object in the rights array that contains values "messenger" for module and "writeToResident" for right.

I tried many things like

const setter = {};
setter[`rights.$.${module}.${right}`] = value;
DefaultRoles.update(defaultRoleId, {
	$set: setter
});

// OR

const setter = {};
setter[`rights.${module}.${right}`] = value;
DefaultRoles.update(defaultRoleId, {
	$set: setter
});

But it doesn't work. Is it even possible to do this ?

EDIT

With your help I tried this:

DefaultRoles.update({
  "_id": defaultRoleId,
  "rights.module": module,
  "rights.right": right
}, {
  $set: {
    'rights.$.default': value,
  }
});

But something very strange happened: sometime the edited object is not the right one. For exemple, when I update module: "manual" right: "write", module: "actuality" right: "write" is updated instead.

2
  • 1
    To point you in the right direction, your query would be DefaultRoles.update({ '_id': defaultRoleId, 'rights.module': 'messenger', 'rights.right': 'writeToResident' }, { '$set': { 'rights.$.default': value } }); Commented Nov 29, 2017 at 9:21
  • I followed your instructions but there are some bugs with it... @chridam Commented Nov 29, 2017 at 10:00

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.