MongoDB database command operations

Source: Internet
Author: User
Tags install mongodb

In the previous section, we explained some understanding and concepts of mongodb (see for details). Next, we will step into mongodb learning.

Unlike apsaradb for mongodb, which has powerful GUI clients, apsaradb for mongodb also has powerful functions and stability, therefore, most operations on mongodb are performed using commands similar to cmd commands (called shell operations in mongodb). Therefore, learning mongodb shell operations is an important foundation.

I. Preparations

1. Download mongoDB

: Http://www.mongodb.org/downloads

Select the appropriate version

Related documents: http://www.mongodb.org/display/DOCS/Tutorial

2. Install mongoDB

A. Pressure-free mode:

Open the downloaded mongodb-xxx.zip file, find the bindirectory, and run mongod.exe to start the service. The default port is 27017, And the db is saved to the/data/db directory of the root directory of the system C hard drive directory. Also, if your mongodb-xxx.zip file is on the E drive, you need to create a data/db directory under drive C. MongoDB will not create this directory for you.

Then run mongo to connect to the test database, and you can perform data operations. Run help to display the help command line.

B. Decompression Mode

Decompress the downloaded mongodb-xxx.zip file to any directory, find the bindirectory, and run mongod.exe to start mongoDB. The default port is port 27017, And the db is saved in the/data/db directory of the root directory of the hard disk directory where the zip file is located. Also, if your mongodb-xxx.zip file is on an elastic drive, you need to create a data/db directory under the elastic drive. MongoDB will not create this directory for you.

Then run mongo to connect to the test database, and you can perform data operations. Run help to display the help command line.

3. Simple Test

  1. >2 + 4
  2. 6
  3. >Db
  4. Test
  5. >// The database is created when data is inserted for the first time.
  6. Fri May 20 16:47:39 malformed UTF-8 character sequence at offset 27
  7. Error2 :( shellhelp1) exec failed: malformed UTF-8 character sequence at offset 27
  8. >Db. foo. insert ({id: 2011, userName: 'hoojo ', age: 24, email: "hoojo_@126.com "});
  9. >Db. foo. find ();
  10. {"_ Id": ObjectId ("4dd62b0352a70cbe79e04f81"), "id": 2011, "userName": "hoojo ",
  11. "Age": 24, "email": "hoojo_@126.com "}
  12. >

The preceding simple operations show the currently used database, and add and query data.

Ii. Database shell Data Operations

The shell command operation syntax is similar to that of JavaScript. In fact, the underlying query statements on the console are all completed using JavaScript scripts.

Ø Database

1. Help to view command prompts

  1. >Help
  2. >Db. help ();
  3. >Db. yourColl. help ();
  4. >Db. youColl. find (). help ();
  5. >Rs. help ();

2. Switch to/create a database

  1. >Use yourDB;

When a table is created, the current database is automatically created.

3. query all databases

  1. >Show dbs;

4. Delete the currently used database

  1. >Db. dropDatabase ();

5. clone a database from a specified host

  1. >Db. cloneDatabase ("127.0.0.1 ");

Clone the database data on the specified machine to the current database

6. Copy the specified database data from the specified machine to a database

  1. >Db. copyDatabase ("mydb", "temp", "127.0.0.1 ");

Copy the mydb data of the local machine to the temp database.

7. Fix the current database

  1. >Db. repairDatabase ();

8. view the currently used database

  1. >Db. getName ();
  2. >Db;

The db and getName methods have the same effect. You can query the currently used database.

9. display the current db status

  1. >Db. stats ();

10. Current db version

  1. >Db. version ();

11. view the address of the linked machine of the current db

  1. >Db. getMongo ();

Collection aggregation set

1. Create an aggregation set (table)

  1. >Db. createCollection ("collName", {size: 20, capped: 5, max: 100 });

2. Get the clustering set (table) with the specified name)

  1. >Db. getCollection ("account ");

3. obtain all the aggregation sets of the current db.

  1. >Db. getCollectionNames ();
4. display the status of all clustered indexes of the current db
  1. >Db. printCollectionStats ();

User-related

1. Add a user

  1. >Db. addUser ("name ");
  2. >Db. addUser ("userName", "pwd123", true );

Add a user, set a password, and check whether the user is read-only

2. database authentication and security mode

  1. >Db. auth ("userName", "123123 ");

3. display all current users

  1. >Show users;

4. delete a user

  1. >Db. removeUser ("userName ");
  • 1
  • 2
  • 3
  • 4
  • Next Page

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.