문제

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?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top