Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common commands of MongoDB

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 290

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report