Mongodb basic operations

Source: Internet
Author: User

1. Installation: Take my local machine as an example:

1) command installation method:

1.1 decompress the downloaded package to D: \ Installed \ MongoDB.

1.2 open the command prompt as an administrator and switch the directory to the D: \ Installed \ MongoDB \ bin directory (you can also set the environment variable so that you do not need to switch the directory ).

1.3 run the following command

Mongod -- dbpath D: \ Installed \ MongoDB \ data \ db -- logpath D: \ Installed \ MongoDB \ log \ mylog. log -- install -- serviceName "MongoDB"

After the installation is complete, enter the following URL in the browser: http: // localhost: 27017/. If the following content appears, the installation is successful:

You are trying to access MongoDB on the native driver port. For http diagnostic access, add 1000 to the port number

Command explanation: -- dbpath: storage location of mongodb database files; -- logpath: Location of mongodb log files -- service name of serviceName mongodb

 

2) Configuration File Installation (recommended)

2.1 create a configuration file named mongod. cfg (the name is not mandatory) and place it under D: \ Installed \ MongoDB (the location is not mandatory). Write the following content to the configuration file:

Dbpath = D: \ Installed \ MongoDB \ data \ db

Logpath = D: \ Installed \ MongoDB \ log \ mongod. log

The content is the configuration information during mongodb installation.

2.2 enter the command prompt as an administrator and run the following command:

Mongod -- config d: \ mongodb \ mongod. cfg -- install

After the installation is successful, the service name defaults to MongoDB.

 

==================================================== Common commands and indexes of mongodb operation

Common commands: ==== view the current database status db. stats () === the personalized system can help Mark slow queries. The command to enable it is as follows: db. setProfilingLevel (1) = view personalized data, and output all the log databases whose query time exceeds Ms. system. profile. find ({millis :{$ gt: 100 }}) = view copy information rs. status () === view lock status use configdb. locks. find () ========= Database Backup steps: 1. Write the data in the cache to the disk, lock the database, and prohibit writing data to the database. fsynclock () 2. Create snapshot 3. Unlock the database. fsyncUnlock () ======= create an index ensureIndex () = Example 1: {"field1": 1 }{ "field2, field3": 1 }{ "field2 ": 1, "field3": 1} indexes can also be created on sub-documents, such as db. factorie S. find ({metro: {city: "New York", state: "NY" }}) = Example 2: people class: {"_ id": ObjectId (), "name": "john smith", "address": {"street": "Main", "zipcode": 100081, "state ": "WI"} index creation statement: db. people. ensureIndex ("address.zip code": 1) = collection of the products combination index: {"_ id": ObjectId (...), "item": "Banana", "category": ["food", "produce", "grocery"], "location": "4th Street Store", "stock ": 4, "type": cases, "arrival": Date (...)} statement for creating a composite index: d B. products. ensureIndex ({"item": 1, "location": 1, "stock": 1}) = the value of a field indexed by multiple keys is an array, the index created on this field will index every element in the array. Example: {"_ id": ObjectId ("... ")," name ":" Warm John "," author ":" li ke "," tags ": [" weather "," hot "," record ", "l"] }== unique index: Example: create a unique index for user_id: db. addresses. ensureIndex ({"user_id": 1}, {unique: true}) === sparse indexdb. addresses. ensureIndex ({"xmpp_id": 1}, {sparse: true}) =========== parameters for creating an index === create an index in the background, does not affect the use of db for database instances. people. ensureIndex ({zipcode: 1}, {background: true, sparse: true}) = force the creation of a unique index db. accounts. ensureIndex ({username: 1}, {unique: true, dropDups: true })

Duplicate records will be deleted when force creation is performed!

 

 

 

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.