Mangodb commonly used query statements_notes


Preface

The Mangodb database will be used in my work, which is a non-relational database, so some of its query statements will be different from sql, and will not be able to be written immediately for a while, so I made some notes, hoping to make some output content .

1. Introduction to Mangodb

Insert picture description here

MangoDB and SQL noun comparison
Insert picture description here

Two, Mangodb commonly used query statements

The version of mangodb used is 3.4.16,
a collection named user, which is equivalent to a table in Mysql

1. Find all data in the user collection: db.user.find() is
equivalent to select * from user in sql

2. Find the name deduplication data in the user collection: db.user.distinct('name') is
equivalent to select distinct name from user in sql

3. Find the specified column name, age data in the user collection: db.user.find({},{name :1 ,age:1}) is
equivalent to select name, age from user in sql

4. Query data with age=22: db.user.find({age:22}) is
equivalent to select * from user where age =22 in sql

5. Query records with age greater than 22: db.user.find({age: {$gt:22} }) is
equivalent to select * from user where age >22

6. Query records with age greater than or equal to 22: db.user.find({age: {$gte:22} }) is
equivalent to select * from user where age >=22

7. Query records with age less than 22: db.user.find({age: {$lt:22} }) is
equivalent to select * from user where age <22

8. Query records with age less than or equal to 22: db.user.find({age: {$lte:22} }) is
equivalent to select * from user where age <=22

比较符:大于($gt),大于等于($gte),小于($lt),小于等于($lte)

9. Query data older than 22 years old and younger than 26 years old:

db.user.find(age:{$gte:22,$lte:26 })

Equivalent to select * from user where age>22 and age<26 in sql

10. Query the data that contains mango in name: db.user.find({name:/mango/}) is
equivalent to select * from user where name like “%mango%” in sql

11. Query the data starting with mango: db.user.find({name:/^mango/}) is
equivalent to select * from user where name like “mango%”

12. Query the specified column name, age and age>25:
db.user.find({age}:{$gt:25},{name:1,age:1}) is
equivalent to select name, age from user where age >25

13. Query the first five data: db.user.find().limit(5) is
equivalent to select * from user limit 5 in sql

14. Query the data after 10: db.user.find().skip(10) is
equivalent to select * from user where id not in (select * from user) in sql

15. Paging query: db.user.find().limit(10).skip(10) is
equivalent to select * from user limit10 in sql, 10
limit is pagesize, skip is pagenum

16.or query, query data whose age is 22 or 25:
db.user.find({$or:[{age:22},{age:25}]}) is
equivalent to select * from user where age in sql =22 or age = 25

17. Query the number of records in a result set
db.user.find({age:{$gte:25} }).count()
select count(*) from user where age >=25

18. Not equal to query:
db.user.find({author:{$ne:a}}) is
equivalent to select * from user where author <> a in sql

19. Ascending order, descending order Sort
in ascending order by age: db.user.find().sort({age}:1)
Sort in descending order by age: db.user.find().sort({age}:-1)
1 is Ascending order, -1 is descending order

20. Query the data of the url field under the dimlist field (the url field is nested in the list of the dimlist field).
Baidu hasn't found out how to write this sentence for a long time.
Dashen colleagues wrote it out and learned db.getcollection( 'user').find('dimlist.url':{$exists:true})

to sum up

The above is the content shared today. This article only briefly introduces the use of Mangodb database query statements that I often use in my work. There are also some aggregation operations that will be updated as much as possible later. Please criticize and correct any errors.

Guess you like

Origin blog.csdn.net/weixin_43785299/article/details/108130739