MongoDB Getting Started Guide and example

Source: Internet
Author: User
Tags install mongodb

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 + 46> dbtest> // The database Fri May 20 16:47:39 malformed UTF-8 character sequence at offset 27error2 (shellhelp1) exec failed will be created when data is inserted for the first time: 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.

Recommended reading:

Install MongoDB2.4.3 in Ubuntu 13.04

How to create a new database and set in MongoDB

MongoDB beginners must read (both concepts and practices)

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. The Help command prompts helpdb. help (); db. yourColl. help (); db. youColl. find (). help (); rs. help (); 2. Switch/create database> use yourDB; when creating a set (table), the current database is automatically created. 3. query all databases; 4. Delete the currently used database. dropDatabase (); 5. Clone the database db from the specified host. cloneDatabase ("127.0.0.1"); clone the database data on the specified machine to the current database 6. copy the data from the specified machine to a database. copyDatabase ("mydb", "temp", "127.0.0.1"); copy the mydb data of the local machine to the temp database. 7. Fix the current database. repairDatabase (); 8. view the currently used database. getName (); db; the db and getName methods have the same effect. You can query the currently used database 9 and display the current db status. stats (); 10. Current db version. version (); 11. view the address of the linked machine db of the current db. getMongo ();
Collection aggregation set


1. Create an aggregation set (table) db. createCollection ("collName", {size: 20, capped: 5, max: 100}); 2. Get the clustered set (table) db with the specified name. getCollection ("account"); 3. obtain all the clustered db of the current db. getCollectionNames (); 4. display the status of all clustered indexes of the current db. printCollectionStats ();
User-related


1. Add a user database. addUser ("name"); db. addUser ("userName", "pwd123", true); add a user, set a password, whether to read-only 2, database authentication, security mode db. auth ("userName", "123123"); 3. show all current users; 4. delete user db. removeUser ("userName ");
Other


1. query the previous error message db. getPrevError (); 2. Clear the error record db. resetError ();

  • 1
  • 2
  • 3
  • 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.