Getting Started with MongoDB

Source: Internet
Author: User

Installation of 1.MongoDB

Step one: Download MongoDB

Download the installation package: Http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.4.tgz

Step two: Set up the MONGODB program storage directory

Unzip it to/apps, then rename it to MONGO, and the path is/apps/mongo

Step three: Set the data file storage directory

Create a directory of/data/db, mkdir–p/data/db

Step four: Start the MongoDB service

In Terminal input:/apps/mongo/bin/mongod–dppath=/data/db

Step five: Randomly start MongoDB as a Linux service

Create a/apps/mongo/logs/mongodb.log file to store the MongoDB log file vi/etc/rc.local, open the configuration file with the VI editor, and add the following line of code to it

/apps/mongo/bin/mongod--dbpath=/data/db--logpath=/apps/mongo/logs/mongodb.log

Step Six: Client connection verification

In terminal input:/apps/mongo/bin/mongo, if it appears

MongoDB Shell version:2.0.4

Connecting To:test

Indicates that MongoDB has been successfully installed

2.MongoDB Common Commands

(1) Database commands

A) Add user db.adduser (' name ', ' pwd ')

b) Delete user db.removeuser (' name ')

c) User authentication Db.auth (' name ', ' pwd ')

d) Copy database, copy db.copydatabase from Test library (' Test ', ' mydb ')

e) Delete Database Db.dropdatabase ()

f) Get all the collections under the Database Db.getcollectionnames ()

g) Stop MONGO Service Db.shutdownserver ()

h) using a database use dbname

i) View all collections under the database Show collections

j) View all DB show DBS

k) View current database status Db.stats ()

L) View User show Users

m) View Help Db.help ()

(2) Set operation

A) adding data, adding the first data to the collection creates the collection, assuming that the Char collection is created Db.char.save ({' name ': ' Weixiaobao ', ' age ': 23})

b) Storage Array object Db.char.save ({' name ': ' name123 ', ' addr ': [' Beijing ', ' Nanjing ']})

c) query all, query a db.char.find () Db.char.findOne ()

d) Conditional query (query the record named ' Weixiaobao ') db.char.find ({' name ': ' Weixiaobao '})

e) Limit the number of query bars (starting from the second bar to 10) db.char.find ({' name ': ' Weixiaobao '}). Skip (2). Limit (10)

f) The query returns the number of bars Db.char.find ({' name ': ' Weixiaobao '}). Count ()

g) sort (by name ascending, age descending) Db.char.find (). Sort ({' name ': 1, ' age ':-1})

h) Query the specified column to db.char.distinct (' name ')

i) Remove the set Db.char.drop ()

j) Create index Db.char.ensureIndex ({' Name ': 1, ' Age ': 1},{unique:true})

k) View index db.char.getIndexes ()

L) Delete index Db.char.dropIndex (' IndexName ')

m) Delete data db.char.remove ({' name ': ' Weixiaobao '})

N) Update data db.char.update ({' name ': ' Weixiaobao '},{' $set ': {' age ': 40}})

O) Condition query Db.char.find ({' age ': {$gt: 20}})

Getting Started with MongoDB

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.