Jivan Jivan - 1 year ago 223
Python Question

MongoEngine delete document

I have the following MongoEngine document

'_id': 'some_id',
'data': 'some_data'

How can I
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

# mongoengine.errors.ValidationError: 'None' is not a valid ObjectId

# TypeError: delete() takes 1 positional argument but 2 were given

# mongoengine.errors.ValidationError: 'None' is not a valid ObjectId

-- note

Oddly enough, the following works perfectly:

# delete all documents in the collection

But I've followed MongoEngine docs, and still can't manage to delete just one specific document.

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download