Issue
I have the following MongoEngine document
{
'_id': 'some_id',
'data': 'some_data'
}
How can I delete this document using MongoEngine?
What I've tried:
import my_collection
obj = my_collection.MyCol.objects.get(_id='some_id')
# obj is correctly found - let's continue
obj.delete()
# mongoengine.errors.ValidationError: 'None' is not a valid ObjectId
obj.delete('some_id')
# TypeError: delete() takes 1 positional argument but 2 were given
obj.delete(_id='some_id')
# mongoengine.errors.ValidationError: 'None' is not a valid ObjectId
-- note
Oddly enough, the following works perfectly:
my_collection.MyCol.objects.delete()
# delete all documents in the collection
But I've followed MongoEngine docs, and still can't manage to delete just one specific document.
Solution
If your document overrides _id, you must indicate that it's still the primary key. Change your document class definition from:
class MyCol(Document):
_id = db.StringField()
...
To specify the primary key:
class MyCol(Document):
_id = db.StringField(primary_key=True)
...
Answered By - Jace Browning Answer Checked By - David Marino (Python.Easybugfix Volunteer)
Post a Comment
Post a Comment