Common Mongo client commands

Source: Internet
Author: User
Tags mongo shell
Mongo client Common commands 1. database related commands 1showdbs list all databases 2usememo Use Database memo. Even if the database does not exist, it can be executed, but the database will not be created immediately. The database will be created only when insert and other operations are executed. www.2cto. com3showcollections

Mongo client Common commands 1. database related commands 1 show dbs // list all databases 2 use memo // use the database memo. Even if the database does not exist, it can be executed, but the database will not be created immediately. It will not be created until the insert operation is executed.


Common Mongo client commands

I. database commands

1 show dbs // list all databases

2 use memo // use the database memo. Even if the database does not exist, it can be executed, but the database will not be created immediately. It will not be created until the insert or other operations are executed.

Www.2cto.com

3 show collections // list the collections of the current database

4. view the status of each collection database. printCollectionStats ()

5 db // display the current database

6 show users // List users

7 db. system. users. find () // List users

8 db. removeUser ('user1') // delete a user

9 db. c1.drop () // Delete the set c1

10 db. dropDatabase () // Delete the current database II. security and authentication

1 use shine // if the root permission is required, the admin database is used.

Www.2cto.com

2 db. addUser ("username", "password") // common permission, read/write

3 db. addUser ("username", "password", true) // read only, not writable

4 db. system. users. remove ({user: username}) // delete user 1 db. c1.save ({name: "zhangsan", age: 18}) // if no primary key is written, the system automatically generates a primary key named _ id, 2. each document in MongoDB has a _ id field as its first attribute. This value is usually a BSON Object id. Therefore, this id is unique for each member in the set, if you insert a document without providing an id, the database automatically generates an id and stores it in the _ id field.

3. db. c1.save ({_ id: 1, name: "lisi", age: 22}) // enter the id Primary Key value by yourself. The id primary key value can be a string or a number.

4. db. c1.save ({"name": "MongoDB", "type": "database", "count": 1, "info": {x: 203, y: 102 }}) 4. Create an index:

1 coll. ensureIndex ({productid: 1}) // create a common index on productid

2 coll. ensureIndex ({district: 1, plate: 1}) // multi-field Index

3 coll. ensureIndex ({productid: 1}, {unique: true}) // unique index

4 coll. ensureIndex ({productid: 1}, {unique: true, dropDups: true |) // when creating an index, if the index field value already exists, delete the duplicate record

5 coll. getIndexes () // view the index

6 coll. dropIndex ({productid: 1}) // delete a single index 1. db. coll. find () // select * from coll

2. db. coll. find (). limit (10) // select * from coll limit 10

3. db. coll. find (). sort ({x: 1}) // select * from coll order by x asc

4. db. coll. find (). sort ({x:-1}) // select * from coll order by x desc

5. db. coll. find (). sort ({x: 1 }). skip (5 ). limit (10) // select * from coll order by x asc limit 5, 10

6. db. coll. find ({x: 10}) // select * from coll where x = 10

7. db. coll. find ({x: {$ lt: 10}) // select * from coll where x <= 10

8. db. coll. find ({}, {y: true}) // select y from coll

9. Access data through a cursor

.> Var cursor = db. collect1.find ();

> While (cursor. hasNext () printjson (cursor. next ())

When there are more than 20 rows of data, use the it command to view more data through forEach

10> db. collect1.find (). forEach (printjson)

11. In mongo shell, you can consider the cursor as an array.

12> var cursor = db. collect1.find ();

13.> printjson (cursor [4])

14 {"_ id": ObjectId ("4c691e72ed2a47b462dfa806"), "x": 4, "y": 3}

Note that all data before cursor [4] is loaded to the memory at the same time. This operation is inappropriate for a large result set, memory overflow occurs. When a large amount of data is queried, the cursor should be used as an iterator.

Www.2cto.com

Mongo does not support SQL statements.

1. coll. find ({"address. city": "gz"}) // search records with the city Value of gz in the address of the nested document

2. coll. find ({likes: "math"}) // search for an array

3. coll. ensureIndex ({"address. city ": 1}) // create 1 in the fields of the nested document. db. user. update ({uid: 1}, {$ set: {age: 26}) // update user set age = 26 where uid = 1

2. db. user. update ({uid: 1}, {$ inc: {age: 1}) // update user set age = age + 1 where uid = 1. db. user. delete ({uid: 1}) // delete user where uid = 1

2. db. Position. remove ({"id": 10}) // delete * from Position where id = 10 1. // json or csv format, each time a collection

Export export-d producttrade-c basic-o/home/data/export _backup/producttrade_100504.json

145.3. Export Import-d producttrade-c basic -- drop/home/data/export _backup/producttrade_100504.json // binary data format, often used for backup and Restoration

2 mongodump-d shine-o/home/data/mongo_backup

3 mongorestore-d shine -- drop/home/data/mongo_backup/shine

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.