0

I have a document like this

{
    users: [
        {
            name: 'John',
            id: 1
        },
        {
            name: 'Mark',
            id: 2
        },
        {
            name: 'Mike',
            id: 3
        },
        {
            name: 'Anna',
            id: 4
        }
    ]
}

and I want to remove users from the array with ids 2 and 4. To do that I execute the following code:

const documents = [
    {
        id: 2
    },
    {
        id: 4
    },
]
Model.updateOne({ document_id: 1 }, { $pull: { users: { $in: documents } } });

But it doesn't remove any user.

Could you say me what I'm doing wrong and how to achieve the needed result?

0

1 Answer 1

2

This works if you can redefine the structure of your documents array:

const documents = [2, 4]
Model.updateOne({ document_id: 1 }, { $pull: { users: { id: { $in: documents } } } })
Sign up to request clarification or add additional context in comments.

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.