0

I have an array of objects that contains metadata and looks similar to this.

Data:

metadata:[ 
  { matchid: '1', region: 'europe' }, 
  { matchid: '2', region: 'africa' },
  { matchid: '3', region: 'asia' },
]

I have an endpoint setup to receive an array of IDS ['1', '2'] which would the remove all the objects containing these IDS.

This is my current query:

Query to remove objects

xx.findByIdAndUpdate(
    id,
    $pullAll: {
        "metadata.matchid": {
           $in: req.body.matches
        }
    }
)

I am expecting both objects with the ids of 1 and 2 to be removed

Expected Results:

metadata:[ 
  { matchid: '3', region: 'asia' },
]

I am recieving an error I have never seen before it is an object that says codeName: "BadValue"

1
  • $pullAll removes only the elements in the array that match the specified value exactly you given, including order. And it starts with an empty operand expression ( { } ) . So, maybe query like this: xx.findByIdAndUpdate(id, { $pullAll: { "metadata.matchid": [{ $in: req.body.matches }], }, });. Commented Aug 9, 2021 at 20:25

1 Answer 1

1

As documentation says:

The $pullAll operator removes all instances of the specified values from an existing array. Unlike the $pull operator that removes elements by specifying a query.

$pullAll requires and exact match and $pull is like to use a filter. So you can use $pull in this way.

yourModel.findByIdAndUpdate(
    id,
    $pull: {
        metadata:{
           matchid: { $in: req.body.matches}
        }
    }
)

Example here

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.