Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the common commands of MongoDB

Shulou Source: shulou.com Published: 2022-06-02 02:52:42 09月14日 Update

What are the common commands of MongoDB? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. Database-related

1. Switch / create database

> use "dbname"

two。 Query all databases

> show dbs;mytest 0.000GB

3. View the database currently in use

> db.getName ()

Mytest

4. View database version

> db.version ()

4.2.8

5. View the link address of the current db

> db.getMongo ()

Connection to 127.0.0.1:27017

Second, user-related

1. Create an ordinary user (create a user cg with read and write permissions to the mytest database)

> db.createUser ({user: "cg", pwd: "lianshi", roles: [{role: "readWrite", db: "mytest"}]})

2. Delete user > db.dropUser ("yonghu")

3. Modify the user's password

Db.updateUser ("cg", {pwd: "123456"})

4. Enter data mytest, user name and password authentication

> db.auth ("cg", "lianshi"); third, set Collection correlation

1. Get data aggregation (table)

> db.getCollectionNames (); ["student"]

two。 Collection (table) insert data

Db.student.insert ({"id": "2", "name": "yxy"})

3. Query data

> db.student.find (); {"_ id": ObjectId ("5eef61f3447efbc4346fbb9b"), "id": "2", "name": "yxy"} {"_ id": ObjectId ("5eef61fe447efbc4346fbb9c"), "id": "1", "name": "hmf"} {"_ id": ObjectId ("5eeff9582e8cdcf5c32c0ecf"), "id": "3", "name": "yx"} is equivalent to: select* from student

4. Query unique field values

> db.student.distinct ("name"); ["hmf", "yx", "yxy"]

Will filter out the same data in name

Equivalent to: select distict name from student

5. Query the records of name = yxy

> db.student.find ({"name": "yxy"}); {"_ id": ObjectId ("5eef61f3447efbc4346fbb9b"), "id": "2", "name": "yxy"} {"_ id": ObjectId ("5ef077145c4ca32ccc787893"), "id": "2", "name": "yxy"}

Equivalent to: select * from student where name = "yxy"

6. Insert data of int32 field type

Db.student.insert ({"id": NumberInt (1234567), "name": "hu"})

7. Insert int64 field type data

Db.student.insert ({"age": NumberLong (22), "name": "hu"})

8. Insert Decimal field type data

Db.student.insert ({"va": NumberDecimal ("22.3"), "name": "hu"})

9. Query statement

Db.student.find ({}) .sort ({_ id:-1}) .limit (100)

10. Delete (collection) table

Db.student.drop (): after reading the above, have you mastered the common commands of MongoDB? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Data database user query field type command common content password method more problem ordinary same helpless for this reason address for this Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno Shulou Tech Info Shulou Technology Linux