The database profiler collects fine grained data about MongoDB write
operations, cursors, database commands on a running mongod
instance. You can enable profiling on a per-database or per-instance
basis. The profiling level is also
configurable when enabling profiling. The profiler is off by default.

You can enable database profiling from the mongo shell or
through a driver using the profile command. This section
will describe how to do so from the mongo shell. See your driver documentation if you want to control the profiler from
within your application.

When you enable profiling, you also set the profiling level. The profiler records data in the
system.profile
collection. MongoDB creates the system.profile collection in a database after you
enable profiling for that database.

To enable profiling and set the profiling level, use the
db.setProfilingLevel() helper in the mongo shell,
passing the profiling level as a parameter. For example, to enable profiling
for all database operations, consider the following operation in the
mongo shell:

db.setProfilingLevel(2)

The shell returns a document showing the previous level of profiling.
The "ok":1 key-value pair indicates the operation succeeded:

The threshold for slow operations applies to the entire
mongod instance. When you change the threshold, you change it
for all databases on the instance.

Important

Changing the slow operation threshold for the database
profiler also affects the profiling subsystem’s slow operation
threshold for the entire mongod instance. Always set the
threshold to the highest useful value.

By default the slow operation threshold is 100 milliseconds. Databases with a profiling level
of 1 will log operations slower than 100 milliseconds.

To change the threshold, pass two parameters to the
db.setProfilingLevel() helper in the mongo shell. The first parameter sets the
profiling level for the current database, and the second sets the default
slow operation threshold for the entiremongodinstance.

For example, the following command sets the profiling level for the current
database to 0, which disables profiling, and sets the
slow-operation threshold for the mongod instance to 20
milliseconds. Any database on the instance with a profiling level of 1
will use this threshold:

The slowms field indicates how long an operation must exist in
milliseconds for an operation to pass the “slow” threshold. MongoDB
will log operations that take longer than the threshold if the
profiling level is 1. This document returns the profiling level in
the was field. For an explanation of profiling levels, see
Profiling Levels.

The database profiler logs information about database operations in the
system.profile collection.

To view profiling information, query the system.profile collection. You can use
$comment to add data to the query document to make it
easier to analyze data from the profiler. To view example queries, see
Profiler Overhead.

On a database that has profiling enabled, the showprofile helper
in the mongo shell displays the 5 most recent operations
that took at least 1 millisecond to execute. Issue showprofile
from the mongo shell, as follows:

When enabled, profiling has a minor effect on performance. The
system.profile collection is a
capped collection with a default size of 1 megabyte. A
collection of this size can typically store several thousand profile
documents, but some application may use more or less profiling data per
operation.