Question

I am trying to delete a record from pymongo and I cannot understand the reason that the record is still in my connection.

Here is my code:

_id = bottle.request.forms.get('_id')
collection.remove({'_id':_id})

the _id variable is an input text from an HTML form in my website. Here is the output of the remove method.

{u'connectionId': 86, u'ok': 1.0, u'err': None, u'n': 0}

I cannot see any error, but the record is still in my collection. Any clue?

Was it helpful?

Solution

You need to convert _id variable (that's of type str) to ObjectId type first

from bson.objectid import ObjectId
if type(_id) is not ObjectId:
    _id = ObjectId(_id)

Take a look: http://api.mongodb.org/python/2.7rc0/tutorial.html#querying-by-objectid

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top