MongoDB Use (Mongoshell)

Source: Internet
Author: User
Tags mongo shell

1 overview

The MONGO Shell is an interactive JavaScript interface for MongoDB (that is, JavaScript syntax can be used in this shell). The user is able to perform operations such as querying, updating data through the MONGO shell. The MONGO shell is a component of MongoDB that can be connected to a MongoDB instance via the MONGO shell once the user installs and starts the MongoDB service.

1.1 Starting the MONGO shell

After installing MongoDB, add the Bin folder path to the PATH environment variable, and enter MONGO directly in the command window to launch the MONGO shell. Before you start, make sure that the MongoDB service is started. If the user enters MONGO and does not have any parameters, the MONGO shell is connected to localhost:27017 by default.

MONGO       //After installing MongoDB, add the Bin folder path to the PATH environment variable

1.2 Using the MONGO shell

① Displays the database that the user is using, enter DB:

DB     //This operation will return the default database test, if you need to use a different database, use use<db>
Use test

To view the available databases, you can use the command show DBS, or you can access another database without switching the current database context by using the Db.getsiblingdb () method.

Show Dbsdb.getsiblingdb (ARG)   //arg represents the database name

The user can also create a database and store data at once by command, for example, by creating a new database and a collection (collection, which can be understood as a table in a relational database) and performing an insert (insert)) operation.

Use MyDBdb.myCollection.insert ({x:1})

If you enter a collection name that includes a blank, the beginning of the letter and other errors in the MONGO shell environment, you can get the desired collection in a different syntax, the command example is as follows:

db["mycollection"]. Find () db.getcollection ("mycollection"). Find ()

1.3 Formatting The results of the output

In the MONGO shell Environment, the Db.collection.find () method returns a cursor to the result set, and if the cursor is not assigned to a variable, the cursor is automatically enumerated 20 times and matches the first 20 results of the query criteria ( Document is equivalent to a record in a relational database), and after entering it commands, 20 results are printed. In order to be able to format the results, you can add the. Pretty () Method:

Db.collection. Find (). Pretty ()

You can also enter commands in the MONGO shell:

Print () prints only, does not format

Print (Tojson (<obj>)) JSON formatted form with effect equal to Printjson ()

Printjson ()

1.4 Multi-line input in MONGO Shell

The MONGO shell will run the user-entered code after the user has lost a row, and (, {, [end of], the next line will automatically appear (...) until the user ends with),},]

1.4 in MONGO shell command Auto-complete, exit shell

You can complete some commands with the TAB key, enter quit () or ctrl-c shortcut to exit the shell environment.

2 Mondodb Database Crud (add-and-revise) operations

2.1 Documents and Collection overview

Note (analogous: similar)

2.2 Queries

Performing curd operations in MONGO basically operates on a separate (single) collection.

For details, please refer to the official website document: https://docs.mongodb.org/manual/core/crud-introduction/

MongoDB Use (Mongoshell)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.