MongoDB Common Commands

Source: Internet
Author: User
Tags mongodb create database

Common commands

1. Get Help commands
Db.help ()
db. Table name. Help ()
db. Table name. Find (). Help ()
Rs.help ()

2. Switch/CREATE Database
Use database name NOTE: MongoDB's database can be used first, then created, and when a collection (table) is created, the current database is automatically created
Example: There is a database for SUTDENTDB, to switch to this database:
Use Studentdb

3. Querying all databases
Show DBS
Example:
> Show DBS
Admin 0.078GB
Local 0.078GB
Studentdb 0.078GB
Test 0.078GB

4. Delete the currently used database
Db.dropdatabase ()
Example:
> Use test
Switched to DB test
> Db.dropdatabase ()
{"Dropped": "Test", "OK": 1}
> Show DBS
Admin 0.078GB
Local 0.078GB
Studentdb 0.078GB

5. View the currently used database
Db.getname ()
Example:
> Use Studentdb
Switched to DB Studentdb
> Db.getname ()
Studentdb

6. Display the current DB status
Db.stats ()

7. Current DB version
> db.version ()
2.6.12

8. See how many tables (clustered collections) are in the database
Show collections



View basic information for a clustered collection (table)
View Help
db. Table name. Help ()

2. Query the number of data bars in the current collection
db. Table name. COUNT ()

3. View the size of the data space
db. Table name. DataSize ()

4. The database where the current aggregation collection resides
db. Table name. GETDB ()

5. Get the status of the current aggregation collection
db. Table name. Stats ()

6. Get the total size of the aggregation collection
db. Table name. TotalSize ()

7. View the aggregate collection storage space size
db. Table name. Storagesize ()

8. View Shard Version Information
db. Table name. Getshardversion ()

9. Renaming a Clustered collection
db. Table name. Renamecollection ("new name")

10. Delete the current clustered collection
db. Table name. Drop ()


Query operations
1. Querying All Records
db. Table name. Find ()
Equivalent: select* from table name;
The default is 20 records per page, and you can query the next page of data with the IT iteration command when the display is not displayed. Note: Type the IT command cannot take ";"

2. The query results filter out duplicate data in a field and then display it
db. Table name. DISTINCT ("Field name")
Example:
> db.student.distinct ("Age")
[20, 40] Note: The data in the age field in the student table is removed and displayed

3. Querying the AGE=22 data
db. Table name. Find ({"Age": 22})
Example:
> Db.student.find ({"Age": 20})
{"_id": ObjectId ("5740dcb29bc83e10802f1cd8"), "name": "Kity", "age": +, "gender": "Womand"}

4. Querying for data older than 22
Greater than sign $GT
> Db.student.find ({"Age": {$gt: 22}})
{"_id": ObjectId ("5740c1419bc83e10802f1cd6"), "name": "Tom", "age": All, "gender": "Male"}
{"_id": ObjectId ("5740dc369bc83e10802f1cd7"), "name": "Jerry", "age": +, "gender": "Male", "Guoji": "China"}
{"_id": ObjectId ("5742263773344acd707888ba"), "name": "Gu Zenghui", "age": +, "gender": "Male", "Course": "Linux"}

5. Query for data with age greater than or equal to 25
Greater than or equal to $gte
Db.student1.find ({age:{$gte: 25}})

6. Query for data with age less than 25
Less than $LT
Db.student1.find ({age:{$lt: 25}})

7. Query for data with age less than or equal to 25
Less than or equal to $lte
Db.student1.find ({age:{$lte: 25}})

8. Query for data between age greater than or equal to 30 and less than or equal to 35
Db.student1.find ({age:{$gte: +, $lte: 35}})

Summarize:
Greater than $GT
Greater than or equal to $gte
Less than $LT
Less than or equal to $lte
9. Query the data that contains student in name
> Db.student1.find ({name:/student/}). Count ()
9916
Note: The document will be displayed as long as it has student.

10. Query the data that begins with G in name
Db.student.find ({name:/^g/})

11. Output the two columns of name and age in the student table
> Db.student.find ({},{name:1,age:1})
{"_id": ObjectId ("5740c1419bc83e10802f1cd6"), "name": "Tom", "Age": 23}
{"_id": ObjectId ("5740dc369bc83e10802f1cd7"), "name": "Jerry", "Age": 30}
{"_id": ObjectId ("5740dcb29bc83e10802f1cd8"), "name": "Kity", "Age": 20}
{"_id": ObjectId ("57418fd97267e7d85d8baf3a"), "name": "Ou Yangfeng"}
{"_id": ObjectId ("57418ff77267e7d85d8baf3b"), "name": "Yang Guo"}
{"_id": ObjectId ("5741901e7267e7d85d8baf3c"), "name": "Guo Jing"}
{"_id": ObjectId ("5742263773344acd707888ba"), "name": "Gu Zenghui", "Age": 25}

12. Output both the name and age columns in the student table and specify the AGE>20
> Db.student.find ({age:{$gt: 20}},{name:1,age:1})
{"_id": ObjectId ("5740c1419bc83e10802f1cd6"), "name": "Tom", "Age": 23}
{"_id": ObjectId ("5740dc369bc83e10802f1cd7"), "name": "Jerry", "Age": 30}
{"_id": ObjectId ("5742263773344acd707888ba"), "name": "Gu Zenghui", "Age": 25}

13. Sort by age
Ascending: Db.student.find (). Sort ({age:1})
Descending: Db.student.find (). Sort ({age:-1})

14. Query for data with the name Jerry and age 30
Db.student.find ({name: "Jerry", age:30})

15. Query the first 5 data
Db.student.find (). Limit (5)

16. Query after 5 data
Db.student.find (). Skip (5)

17. Querying data between rows from the second row to the fifth row
Db.student.find (). Limit (2). Skip (5)

18.or query for data aged 20 or 25
Db.student.find ({$or: [{age:20},{age:25}]})
{"_id": ObjectId ("5740dcb29bc83e10802f1cd8"), "name": "Kity", "age": +, "gender": "Womand"}
{"_id": ObjectId ("5742263773344acd707888ba"), "name": "Gu Zenghui", "age": +, "gender": "Male", "Course": "Linux"}

19. Query the first piece of data
Db.student.findOne ()

20. Statistics using the count () function
Next update!

Index
1. Create an index

Next update!

MongoDB Common Commands

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.