Remove all documents form collection?

128 views
Skip to first unread message

Kevin Burton

unread,
Feb 29, 2016, 1:01:05 PM2/29/16
to mongodb-user
This is definitely a simple question but I couldn't find the answer immediately.

I want to remove all documents from a collection and then remove the collection. So I use the shell command :

> db.formDefinition.remove({})

But I get the result back:

WriteResult({ "nRemoved" : 0 })

Which leads me to believe that nothing was removed.

Ideas?

Wan Bachtiar

unread,
Mar 1, 2016, 3:38:40 AM3/1/16
to mongodb-user

Hi Kevin,

MongoDB should display the number of removed documents. At least in versions: v2.6.11, v3.0.9, and v3.2.3.

Make sure that you execute remove() in the right database i.e. the database that contains the collection you intend to remove.

If you execute remove() in a collection that does not exist it would return nRemoved: 0 

For example:

> db.collection_doesnt_exist.remove({})
WriteResult({ "nRemoved" : 0 })

If you are still having this issue, could you provide the following:

  • The MongoDB server version that you are using.
  • The output of db.collection.stats() before and after you execute remove().

Regards,

Wan.

Kevin Burton

unread,
Mar 1, 2016, 11:06:07 AM3/1/16
to mongodb-user
Thank you. This is helpful.

Is there a separate command to delete/drop the collection? It doesn't seem that removing all the documents also removes the collection.

Thanks again.

Kevin.

Wan Bachtiar

unread,
Mar 1, 2016, 6:21:21 PM3/1/16
to mongodb-user

Hi Kevin,

To remove a collection from a database you could use db.collection.drop(). It also removes any indexes associated with the dropped collection.

For reference of all the available collection methods please see Collection Methods.

I would recommend to enrol in a free online course MongoDB University: M102 - MongoDB for DBAs to find out more about MongoDB. The next session starts on 15th March.

Kind regards,

Wan.

Kevin Burton

unread,
Mar 2, 2016, 8:22:07 AM3/2/16
to mongod...@googlegroups.com
When I do 'db.collection.drop()', I get a return 'false' and nothing is removed.
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/mlbf-t2Q5tg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/adabc67e-820d-4137-bcf5-13c1b7efa61c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wan Bachtiar

unread,
Mar 2, 2016, 6:50:58 PM3/2/16
to mongodb-user

Hi Kevin,

As mentioned in the manual for db.collection.drop(), the command returns false when the collection to drop does not exist.

Note that the word collection in db.collection.drop() is meant to be replaced with your own collection name. So per your example, it’s should be db.formDefinition.drop().
So, the command is actually db.<your collection name>.drop().

Also make sure that you executed db.collection.drop() in the correct database. Check using show collections whether the collection that you are wanting to drop exist in the database you are on. Please see command helpers to see commands such as use <db> or show dbs.

Additionally, please check out the tutorial working with the Mongo Shell

Regards,

Wan.

Kevin Burton

unread,
Mar 2, 2016, 10:04:55 PM3/2/16
to mongod...@googlegroups.com
Thank you. I had a typo in my collection name. All is good.
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/mlbf-t2Q5tg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/9761f04b-58b8-4bf7-af81-22818224a0bc%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages