A
A
Andrew2019-10-26 19:17:34
MongoDB
Andrew, 2019-10-26 19:17:34

How to remove a document from mongodb that contains an array of certain elements?

There is a document that contains an array. To delete this document, you need to pass an array to mongoose with the values ​​in exactly the same order, which is hard to guarantee. That is, the input may contain [ 'bc', ef', gh', 'ab' ], in which case it will not be removed. When I know that this array can have a maximum of two elements, this hack works:
someField: [ 'ab', 'cd', 'ef', 'gh' ]

schema.findOneAndDelete( { $or: [ { someField: [ 'ab', 'cd' ] }, { someField: [ 'cd', 'ab' ] } ] })

But if there are more elements, then this will no longer work. There must be some mechanism in mongo/mongoose for this.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IDONTSUDO, 2019-10-27
@IDONTSUDO

It's too hard a hack.
even with this option, you cannot guarantee that exactly the array you were looking for will be deleted. How not to approach this problem. In any case, we can remove the wrong information.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question