Get the App
SLTechnology News&Howtos  ›  Database  › 

4.MongoDB Command Line

Shulou Source: shulou.com Published: 2022-06-01 22:33:46 10月17日 Update

4.MongoDB Command Line

The latest content will be updated at the origin server. To reprint, please keep the original link: http://dashidan.com/article/mongodb/index.html

Introduction to the ① MongoDB Command Line

The mongo command line is the JavaScript interface of MongoDB database. You can use this command line to add, delete, modify and query the database, performance management and other operations.

The mongo command line is a component of MongoDB. After you start MongoDB, you can use the command line to connect to MongoDB.

② starts the MongoDB command line

Before starting the MongoDB command line, you need to make sure that MongoDB has been started.

Enter the bin directory in the installation of MongoDB.

Windows

Execute mongo.exe

Linux

. / mongo

MongoDB default IP and Port

The default binding IP for MongoDB is 127.0.0.1 and the port is 27017. If the default configuration of mongo is not modified. Executing the mongo command will connect 127.0.0.1 to 27017.

③ mongo displays all databases

Show dbs

④ mongo uses or creates a database

If the specified database does not exist, a database is created.

Use dashdan_com

⑤ mongo shows the current database usage

Db

⑥ mongo insert data

Use dashdan_comdb.myCollection.insertOne ({x: 1})

Description:

Db is the index of the database currently used. Do not write the name of the database here. Is to adopt db.

Name of the myCollection dataset. This can be customized.

⑦ mongo find data

Db.myCollection.find (); if there are spaces or special characters in the name of the dataset, you can get it by db.getCollection ().

Example:

Db.getCollection (myCollection) .find ()

The mongo command line receives a maximum of 4095 characters per line, and the mongo command shortens it beyond this limit.

⑧ mongo command output result formatting

Db.myCollection.find () .pretty ()

You can also set the mongo command to display the results in the following ways:

Print () prints data without formatting

Print (tojson (

) output in JSON format, same as printjson ()

Printjson () output in JSON format, the same as print (tojson (

))

⑨ mongo command multiline mode

If you end with {, [, () on the mongo command line, the next line will begin with. At the beginning, until the corresponding symbol},],) is encountered.

> if (x > 0) {... Count++;... Print (x);...}

You can also type two blank lines in a row to end the statement.

> if (x > 0. >

⑩ mongo Command Line exit

Enter quit (), or press Ctrl-C.

Reference article

Official website article

Tags: Command data database format name output character article method port result input special one line two content original text can be passed that is the beginning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Technology Shulou Tech Info Apple MariaDB