Javatpoint Logo
Javatpoint Logo

MongoDB Database commands

The MongoDB database commands are used to create, modify, and update the database.

#1. db.adminCommand(cmd)

The admin command method runs against the admin database to run specified database commands by providing a helper.

Command: Either the argument is specified in the document form or a string form. If the command is defined as a string, it cannot include any argument.

Example:

Creating a user named JavaTpoint with the dbOwner role on the admin database.

Output:

MongoDB Database commands

#2. db.aggregate()

The aggregate method initialize a specific diagnostic or admin pipeline, which does not require anu underlying collection.

Syntax:

The pipeline parameter does not require any underlying collection and always starts with a compatible stage, such as $currentOp or $listLocalSessions. It is an array of stages that will be executed.

Example:

The following example runs a pipeline with two stages. The first is the $currentOp operation and the second will filters the results.

Output:

MongoDB Database commands

#3. db.cloneDatabase("hostname")

The clonedatabase method copies the specified database to the current database and assumes that the database at the remote location has the same name as the current database.

The hostname parameter contains the hostname of the database that we want to copy.

Example:

db.cloneDatabase("customers")

Output:

MongoDB Database commands

#4. db.commandHelp(command)

We have the help option for the specified database command using the commandHelp method. The command parameter contains the name of a database command.

MongoDB Database commands

#5. db.createCollection(name, options)

A new collection or view will be created using this method. The createCollection method is used primarily for creating new collections that use specific options when the collection is first referenced in a command.

For example - we will create a javaTpoint collection with a JSON Schema validator:


MongoDB Database commands

#6. db.createView()

When we applying the specified aggregation pipeline to the collection, the createView method create a new view for the collection. The method can be computed during the read operations and acts as read-only operations. The views can be created in the same database of the source collection to executes read operations as a part of the underlying aggregation pipeline.

Syntax:

The following example creates a StudentFeedback view with the _id, student.management, and department fields:

Output:

MongoDB Database commands

#7. db.dropDatabase(<writeConcern>)

The drop method removes the specified database and the associated data files.

For example -

we use <database> operation to switch the current database to the temporary database. We use the db.dropDatabase() method to drops the temporary database

#8. db.getLogComponents()

The getLog method returns the current stiltedly settings. The method determines the amount of Log Messages produced by MongoDB for each log message component.

Example:






Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA