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.
Note that this will only work if the document exists in the database and has a valid id.
– Monosyllabic'None' is not a valid ObjectId
- not'some_id'
– Groutprimary_key=True
in the_id
field definition solved the problem - thanks for pointing this out – Grout