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
- >2 + 4
- 6
- >Db
- Test
- >// The database is created when data is inserted for the first time.
- Fri May 20 16:47:39 malformed UTF-8 character sequence at offset 27
- Error2 :( shellhelp1) exec failed: malformed UTF-8 character sequence at offset 27
- >Db. foo. insert ({id: 2011, userName: 'hoojo ', age: 24, email: "hoojo_@126.com "});
- >Db. foo. find ();
- {"_ Id": ObjectId ("4dd62b0352a70cbe79e04f81"), "id": 2011, "userName": "hoojo ",
- "Age": 24, "email": "hoojo_@126.com "}
- >
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
- >Help
- >Db. help ();
- >Db. yourColl. help ();
- >Db. youColl. find (). help ();
- >Rs. help ();
2. Switch to/create a database
- >Use yourDB;
When a table is created, the current database is automatically created.
3. query all databases
- >Show dbs;
4. Delete the currently used database
- >Db. dropDatabase ();
5. clone a database from a specified host
- >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
- >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
- >Db. repairDatabase ();
8. view the currently used database
- >Db. getName ();
- >Db;
The db and getName methods have the same effect. You can query the currently used database.
9. display the current db status
- >Db. stats ();
10. Current db version
- >Db. version ();
11. view the address of the linked machine of the current db
- >Db. getMongo ();
Collection aggregation set
1. Create an aggregation set (table)
- >Db. createCollection ("collName", {size: 20, capped: 5, max: 100 });
2. Get the clustering set (table) with the specified name)
- >Db. getCollection ("account ");
3. obtain all the aggregation sets of the current db.
- >Db. getCollectionNames ();
4. display the status of all clustered indexes of the current db
- >Db. printCollectionStats ();
User-related
1. Add a user
- >Db. addUser ("name ");
- >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
- >Db. auth ("userName", "123123 ");
3. display all current users
- >Show users;
4. delete a user
- >Db. removeUser ("userName ");