Get the App
SLTechnology News&Howtos  ›  Database  › 

Basic operation of mongoDB database

Shulou Source: shulou.com Published: 2022-06-01 12:45:08 09月15日 Update

The collection/coll in mongoDB is equivalent to the table in mysql

The document is equivalent to every row of data in mysql

Common command

1. Get help command

Db.help () db. Table name. Help () db. Table name. Find (). Help () rs.help ()

two。 Switch / create database

Use database name Note: the database of mongoDB can be used first and then created. When you create a collection (table), the current database will automatically create an example: a database is sutdentdb. To switch to this database: use studentdb

3. Query all databases

Show dbs example: > show dbsadmin 0.078GBlocal 0.078GBstudentdb 0.078GBtest 0.078GB

4. Delete the database currently in use

Db.dropDatabase () example: > use testswitched to db test > db.dropDatabase () {"dropped": "test", "ok": 1} > show dbsadmin 0.078GBlocal 0.078GBstudentdb 0.078GB

5. View the database currently in use

Db.getName () example: > use studentdbswitched to db studentdb > db.getName () studentdb

6. Show current db status

Db.stats ()

7. Current db version

> db.version () 2.6.12

8. See how many tables there are in the database (aggregate collections)

Show collections

View basic information about aggregate collections (tables)

View help

Db. Table name .help ()

two。 Query the number of pieces of data in the current collection

Db. Table name .count ()

3. View data space size

Db. Table name .dataSize ()

4. The database where the current aggregate collection resides

Db. Table name .getDB ()

5. Get the status of the current aggregate set

Db. Table name .stats ()

6. Get the total size of the aggregate set

Db. Table name .totalSize ()

7. View the size of the storage space for the aggregate collection

Db. Table name .storageSize ()

8. View Shard version information

Db. Table name .getShardVersion ()

9. Rename an aggregate collection

Db. Table name .renameCollection ("new name")

10. Delete the current aggregate collection

Db. Table name .drop ()

Query operation

1. Query all records

Db. The table name .find () is equivalent to: select* from table name; by default, 20 records are displayed per page, and when the display is not low, you can use the it iterative command to query the next page of data. Note: you cannot type the it command with ";"

two。 The query result will filter out the duplicate data in a field and then display it.

Db. Table name .name ("field name") example: > db.student.distinct ("Age") [20,40] Note: the data in the Age field in the student table is deduplicated and displayed.

3. Query the data of age=22

Db. Table name .find ({"age": 22}) example: > db.student.find ({"age": 20}) {"_ id": ObjectId ("5740dcb29bc83e10802f1cd8"), "name": "kity", "age": 20, "gender": "womand"}

4. Query data with age greater than 22

Greater than sign $gt > db.student.find ({"age": {$gt:22}}) {"_ id": ObjectId ("5740c1419bc83e10802f1cd6"), "name": "Tom", "age": 23, "gender": "male"} {"_ id": ObjectId ("5740dc369bc83e10802f1cd7"), "name": "jerry", "age": 30, "gender": "male" "guoji": "china"} {"_ id": ObjectId ("5742263773344acd707888ba"), "name": "Gu Zenghui", "age": 25, "gender": "male", "Course": "Linux"}

5. Query data with age greater than or equal to 25

Greater than or equal to $gtedb.student1.find ({age: {$gte:25}})

6. Query data with age less than 25

Less than $ltdb.student1.find ({age: {$lt:25}})

7. Query data with age less than or equal to 25

Less than or equal to $ltedb.student1.find ({age: {$lte:25})

8. Query data with age greater than or equal to 30 and less than or equal to 35

Db.student1.find ({age: {$gte:30,$lte:35}})

Summary:

Greater than $gt

Greater than or equal to $gte

Less than $lt

Less than or equal to $lte

9. Query data containing student in name

> db.student1.find ({name:/student/}) .count () 9916 Note: as long as there is a student in the document, it will be displayed.

10. Query data starting with G in name

Db.student.find ({name:/ ^ G /})

11. Output the name and age columns 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}

twelve。 Output the name and age columns in the student table and specify 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 order: db.student.find (). Sort ({age:1}) descending order: db.student.find (). Sort ({age:-1})

14. Query data whose name is jerry and the age is 30

Db.student.find ({name: "jerry", age:30})

15. Query the first five pieces of data

Db.student.find () .limit (5)

16. Query data after 5 items

Db.student.find () .skip (5)

17. Query data between the second row and the fifth row

Db.student.find () .limit (2) .skip (5)

18.or queries data with ages 20 or 25

Db.student.find ({$or: [{age:20}, {age:25}]}) {"_ id": ObjectId ("5740dcb29bc83e10802f1cd8"), "name": "kity", "age": 20, "gender": "womand"} {"_ id": ObjectId ("5742263773344acd707888ba"), "name": "Gu Zenghui", "age": 25, "gender": "male", "Course": "Linux"}

19. Query the first piece of data

Db.student.findOne ()

20. Statistics using the count () function

Indexes

1. Create an index

> db.student.ensureIndex ({name:1}) {"createdCollectionAutomatically": false, "numIndexesBefore": 1, "numIndexesAfter": 2, "ok": 1} > db.student.getIndexes () / / View index [{"v": 1 "key": {"_ id": 1}, "name": "_ id_", "ns": "studentdb.student"}, {"v": 1 "key": {"name": 1}, "name": "name_1", "ns": "studentdb.student"] > db.student.ensureIndex ({name:1,age:1}) {"createdCollectionAutomatically": false, "numIndexesBefore": 2 "numIndexesAfter": 3, "ok": 1} > db.student.getIndexes () [{"v": 1, "key": {"_ id": 1}, "name": "_ id_" "ns": "studentdb.student"}, {"v": 1, "key": {"name": 1}, "name": "name_1" "ns": "studentdb.student"}, {"v": 1, "key": {"name": 1, "age": 1}, "name": "name_1_age_1" "ns": "studentdb.student"}] > db.student.ensureIndex ({name:1,age:-11}) {"createdCollectionAutomatically": false, "numIndexesBefore": 3, "numIndexesAfter": 4, "ok": 1} > db.student.getIndexes () [{"v": 1 "key": {"_ id": 1}, "name": "_ id_", "ns": "studentdb.student"}, {"v": 1 "key": {"name": 1}, "name": "name_1", "ns": "studentdb.student"}, {"v": 1 "key": {"name": 1, "age": 1}, "name": "name_1_age_1", "ns": "studentdb.student"}, {"v": 1 "key": {"name": 1, "age":-11}, "name": "name_1_age_-11", "ns": "studentdb.student"}]

Tags: Data query database example command size field age index between information document version status space switch help output one line five lines Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Information MariaDB Microsoft Apple