Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 682
Joshua Burns Avatar asked Dec 28 '10 22:12

Joshua Burns


People also ask

How do I remove all files from a collection in MongoDB?

To delete all documents in a collection, pass an empty document ( {} ). Optional. To limit the deletion to just one document, set to true . Omit to use the default value of false and delete all documents matching the deletion criteria.

How do you create an index in PyMongo?

Create an index for a MongoDB collection, and specify the order. When you use PyMongo to create an index, you can pair the index name with either a 1 or a -1 integer value to explicitly set the sort order to ascending or descending, respectively.

How do I search in MongoDB?

Find() Method. In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.


1 Answers

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)
like image 154
Lex Avatar answered Oct 14 '22 09:10

Lex