Joshua Burns
Joshua Burns

Reputation: 8582

Removing multiple MongoDB documents in Python

Greetings,

I am attempting to remove multiple documents from a MongoDB collection using the following syntax. I don't know if this is correct as I found it somewhere on the internet and haven't been able to find anything since enforcing the legitimacy of this statement:

pymongo_collection_object.remove(
    [
        {
            'sku': '100-00'
        },
        {
            'sku': '200-00'
        }
    ]
, safe=True)

I would expect the above code would remove both documents including a 'sku' value of '100-00' or '200-00' but unfortunately both documents are still present within the collection. I also tried casting both the 'sku' key and its value to Unicode as I know they are stored in this encoding. As you can tell I am also enabling safe mode ensuring that there is nothing out of line happening on the server side.

Any help is appreciated, thank you!

Upvotes: 10

Views: 4155

Answers (1)

Lex
Lex

Reputation: 1378

You can do so by using $or / $in operators.

Try this:

pymongo_collection_object.remove({'$or': [{'sku': '100-00'}, {'sku': '200-00'}]}, safe=True)

or

pymongo_collection_object.remove({'sku': {'$in': ['100-00', '200-00']}}, safe=True)

Upvotes: 12

Related Questions