[MongoDB]-Shell basic commands

Source: Internet
Author: User

Here, I simply record the basic commands MongoDB uses in the shell client, such as creating/displaying a database, creating a collection, deleting a collection, and so on.

First, start MongoDB and connect to the shell client

Use the Mongod.exe and Mongo.exe commands to start and connect to the database separately, with the following command:

Mongod--127.0.0.1:27017

Ii. Creating a database-use

Using the command use to create the database, you need to be aware that MongoDB is not creating a database at this time, if we exit MongoDB at this time, then we create a database using use can not exist, but will be deleted. The reason is that the database does not have a collection at this time, and is created using use of the database, then MongoDB think this database is useless, it will not be written from memory to the file, shut down the server, of course, will automatically shut down. The command format is as follows:

Use <databaseName>               # # DatabaseName is the name of the database to be created

Iii. Creating a data collection

The creation of data collections in MongoDB can be divided into two ways, the first being to insert data directly into a new, nonexistent collection, which MongoDB automatically creates. The second way is to use the CreateCollection method to create this collection. Here you use the CreateCollection method to create a collection (a simple collection). The command is as follows:

Db.createcollection (<collectionName>)                       # # CollectionName is the name of the data collection to be created

Iv. inserting data records

MongoDB inserts data records using the method Insert, of course, this method has a different parameter rewrite, here insert a record. The command is as follows:

Db.users.insert (<data>)           # # Data to be inserted, data is in Bson format

V. Viewing documents

The View collection data in MongoDB is achieved through the Find method, with different parameters to achieve different effects, here to see all the document data. The command is as follows:

Db.users.find ()

Vi. Deleting document data

In MongoDB, remove the document data from the collection by removing it, and delete all the data here. The command is as follows:

Db.users.remove (query)                     # # query is the Finder and MongoDB will delete the matching data. The method cannot have no parameters. If query is {}, then delete all.

Vii. Updating of document data

MongoDB in the Update method by updating the document, update the parameters of the method is more complex, detailed xxx, here only the simplest operation, first insert a few data, and then in the modification, the command is as follows:

Db.users.update (Query,update)      # # query is the Finder, update is the updater, the query is responsible for querying the matching document data, the updater is responsible for updating.

Viii. Other Orders

Show DBS: View all databases

Show Collections: View all collections under the current database

Help: View the documentation

Db.help (): View Help information at the database level

Db.users.help (): View Help information at the collection level

Db.users.drop (): Delete Collection

Db.dropdatabase (): Delete database

Note: It is assumed that the user collection has a particularly large amount of data, but the collection needs to be emptied, db.users.drop () is recommended instead of using Db.users.remove ({}). After using the Drop method, re-create the set merge to create the index. The advantage is that speed is fast. The reason for this is that remove removes the need for a full-fledged query operation, and the drop operation is not required.

[MongoDB]-Shell basic commands

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.