0

unfortunately I can only find very specific questions on stackoverflow and no exact details in the documentation, so here is a more general example that may help others too.

I just want to add a value to the array (arr) in the object with the "title: 'title3".

{
_id: <id>,
prop1: val1,
prop2: val2,
prop3: [
    {
        title: 'title1',
        arr: ['val1', 'val2', 'val3'],
    },
    {
        title: 'title2',
        arr: ['val1', 'val2', 'val3'],
    },
    {
        title: 'title3',
        arr: ['val1', 'val2', 'val3'], //only update this array
    }
]
}

My current approach looks something like this:

SomeModel.findOneAndUpdate(
    { _id: id, "prop3.title": "title3" },
    {$push: { "prop3.$[].arr": someDoc._id.toString() }}, 
    (err, doc) => {
        if (err) {
            console.log('Error updating doc: ', err);
            resolve(false);
        } else {
            resolve(doc);
        }
    }
);

However, the problem here is that a value is added not only in the array of the object with the title "title3", but everywhere.

How can I add a value exclusively to the array in the object with the title value "title3"? I would also be very grateful for links to documentation explaining this.

2 Answers 2

1

You can use positional $ operator in this way to do it in a single operation:

Using $ you tell mongo "update the object found in the find stage". In this case "update the object where prop3.title is title3".

Note that you are using arrayFilters instead of positional operator.

db.collection.update({
  "_id": 1,
  "prop3.title": "title3"
},
{
  "$push": {
    "prop3.$.arr": "val4"
  }
})

Example here

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much mate, that was exactly what i was looking for
1

This is one solution which may or may not make sense in your full context.

You would have much more luck using the mongoose Document.save() method. Once you have a local copy of the document, you can simply push to the array:

const doc = await SomeModel.findOne({
    _id: id, 
    "prop3.title": "title3" 
});

doc.prop3[3].arr.push(item);
await doc.save();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.