35

How to delete document from MongoDB using Mongoengine? I'veread the API reference here:
http://docs.mongoengine.org/apireference.html
but I can not understand what is:

delete(**write_concern)

Do you have any idea?

1
  • I don't know mongoEngine, but i Think that Write concern is docs.mongodb.org/manual/core/write-concern and fsync: True i think that force write on primary ( journaled ) Commented Jul 31, 2014 at 9:34

2 Answers 2

86

You can either delete an single Document instance by calling its delete method:

lunch = Food.objects.first() // Get a single 'Food' instance
lunch.delete() // Delete it!

Or you can delete all items matching a query like so:

Food.objects(type="snacks").delete()
Sign up to request clarification or add additional context in comments.

3 Comments

You may also want to use lunch = Food.objects.get() if you expect one and only one document to match the query (typically request by ID).
Hey @ross, how can I now what is the status of the delete? means, how can I know it the delete ended with success of failure? anyway to retrieve that status?
@ShaiM. using try catch
-16

U can use the mongoshell and issue the following command:

db.collection.remove({your condition on documents you want to remove})

for example: From food collection you want to remove all the food which has type snacks. then you can issue the following command:

db.food.remove( { type : "snacks" } )

1 Comment

Isn't there any way to do it through mongoengine?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.