Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete system.profile collection from a MongoDB?

I am profiling the db queries in MongoDB. I followed this link. I am trying to delete all data from the collection system.profile so I can start benchmarking different queries again. I tried the following code but it gives an error

CONSOLE SYNTAX

> db.system.profile.remove({})

ERROR

cannot delete from system namespace

How do I delete all data from that collection? If that's not possible, how can I start profiling from the beginning?

like image 379
Nishan Hitang Avatar asked Jul 23 '14 10:07

Nishan Hitang


People also ask

What is system Profile in MongoDB?

MongoDB Profiler is a built-in tool which gives you the actual query level insights. It allows you to analyze all the queries which are being run by the database system.

How do I delete all collections in MongoDB?

To delete all documents in a collection, pass an empty document ( {} ). Optional. To limit the deletion to just one document, set to true . Omit to use the default value of false and delete all documents matching the deletion criteria.

How do I clear my MongoDB database?

db. dropDatabase() the command is used to drop an existing database. This command will delete the currently selected database. If you have not selected any database, then it will delete the default 'test' database.


3 Answers

Firstly, turn off the profiling by setting its level to 0.

db.setProfilingLevel(0)

Then you can simply drop the collection.

db.system.profile.drop()

Now you are free to go and start it all over.

like image 123
Daniel Olszewski Avatar answered Oct 21 '22 15:10

Daniel Olszewski


To riff off of the accepted answer, it is at times helpful to do in a single line:

db.setProfilingLevel(0); db.system.profile.drop(); db.setProfilingLevel(2);

I'm also finding that setting the size of system.profile to 10MB (or larger) rather than its default 1MB is quite helpful:

db.setProfilingLevel(0); db.system.profile.drop(); db.createCollection("system.profile", {capped: true, size: 10 * 1024 * 1024}); db.setProfilingLevel(2);
like image 38
GaTechThomas Avatar answered Oct 21 '22 15:10

GaTechThomas


Keep in mind that the profile collection is capped!

In order to create a collection keeping more records about queries, recreate it with a desired size.

db.setProfilingLevel(0)
db.system.profile.drop()
db.createCollection( "system.profile", { capped: true, size: 1024*1024*10 } )
db.setProfilingLevel(1, { slowms: 500 })
like image 3
DmitrySemenov Avatar answered Oct 21 '22 15:10

DmitrySemenov