当前位置:网站首页>Mongodb commonly used 28 query statements (forward)

Mongodb commonly used 28 query statements (forward)

2022-07-04 14:10:00 Lucky little yellow man

from MongoDB Commonly used 28 Query statements

1、 Check all records

db.userInfo.find();

amount to :

select* from userInfo;

By default, each page displays 20 Bar record , When it doesn't show , It can be used it Iterate the command to query the next page of data . Be careful : type it Orders can't take “;”
But you can set the size of the data displayed on each page , use DBQuery.shellBatchSize= 50; So each page shows 50 The bar is recorded. .

2、 Query the duplicate data of a column in the removed current aggregation set

db.userInfo.distinct("name");

It will filter out name The same data in
amount to :

select distict name from userInfo;

3、 Inquire about age = 22 The record of

db.userInfo.find({"age": 22});

amount to :

select * from userInfo where age = 22;

4、 Inquire about age > 22 The record of

db.userInfo.find({age: {$gt: 22}});

amount to :

select * from userInfo where age > 22;

5、 Inquire about age < 22 The record of

db.userInfo.find({age: {$lt: 22}});

amount to :

select * from userInfo where age < 22;

6、 Inquire about age >= 25 The record of

db.userInfo.find({age: {$gte: 25}});

amount to :

select * from userInfo where age >= 25;

7、 Inquire about age <= 25 The record of

db.userInfo.find({age: {$lte: 25}});

amount to :

select * from userInfo where age <= 25;

8、 Inquire about age >= 23 also age <= 25 Pay attention to the writing style

db.userInfo.find({age: {$gte: 23, $lte: 25}});

amount to :

select * from userInfo where age>=23 and age <= 25;

9、 Inquire about age != 25 The record of

db.userInfo.find({age: {$ne: 25}});
amount to :

select * from userInfo where age != 25;

10、 Inquire about name Contained in the mongo The data of Fuzzy queries are used to search

db.userInfo.find({name: /mongo/});

amount to :

select * from userInfo where name like '%mongo%';

11、 Inquire about name China and Israel mongo At the beginning

db.userInfo.find({name: /^mongo/});

amount to :

select * from userInfo where name like 'mongo%';

12、 Inquire about name China and Israel mongo At the end of the

db.userInfo.find({name: /mongo$/});

amount to :

select * from userInfo where name like ‘%mongo’;

Fuzzy query syntax :{ : /pattern/ }
among options The value can be :

i -- Case insensitive .
m -- matching value There are line breaks in (\n) The circumstances of , Another situation is : Anchors are used in matching rules , The so-called anchor is ^ start , $ ending .
s -- Dot characters are allowed (.) Match all characters , Include line breaks .
x -- Ignore all white space characters .

13、 Query specified column name、age data

db.userInfo.find({}, {name: 1, age: 1});

amount to :

select name, age from userInfo;

Of course name It can also be used. true or false, When used ture And name:1 The effect is the same , If you use false It's to exclude name, Show name Column information other than .

14、 Query specified column name、age data , age > 25

db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});

amount to :

select name, age from userInfo where age > 25;

15、 Sort by age 1 Ascending -1 Descending
Ascending :

db.userInfo.find().sort({age: 1});

amount to :

select * from userInfo order by age asc;

Descending :

db.userInfo.find().sort({age: -1});

amount to :

select * from userInfo order by age desc;

17、 Before query 5 Data

db.userInfo.find().limit(5);

amount to :

select * from userInfo limit 5;

18、 Inquire about 10 Data after

db.userInfo.find().skip(10);

19、 The query in 6-10 strip Data between

db.userInfo.find().limit(10).skip(5);

Can be used for pagination ,limit yes pageSize, The first n When the page skip yes (n-1)*pageSize
amount to :

select * from userInfo limit 5,5;

20、and Inquire about name = zhangsan, age = 22 The data of

db.userInfo.find({name: 'zhangsan', age: 22});

amount to :

select * from userInfo where name = 'zhangsan' and age = 22;

21、or Inquire about

db.userInfo.find({$or: [{age: 22}, {age: 25}]});

amount to :

select * from userInfo where age = 22 or age = 25;

Note that brackets are used between multiple conditions [] Surround .

22、in Inquire about

db.userInfo.find({age :{$in:[22,25]}});

amount to :

select * from userInfo where age in (22,25);

23、 Query the number of records in a result set Statistical quantity

db.userInfo.find({age: {$gte: 25}}).count();

amount to :

select count(*) from userInfo where age >= 20;

skip(), limilt(), sort() When the three are put together , The order of execution is first sort(), And then there was skip(), Finally, it shows limit().

24、 Query the data of a certain period ( The time is of date type , Non string type )

db.userInfo.find({createTime:{$gt:ISODate("2020-11-09T00:00:00Z")}});

amount to :

select * from userInfo where createTime> '2020-11-09 00:00:00';

25、 Perform statistical summation on a field in the table

db.userInfo.aggregate({$group:{_id:null,score:{$sum:"$score"}}})

amount to :

SELECT SUM(score) from userInfo;

26、 Calculate the average value of a field in the table

db.userInfo.aggregate({$group:{_id:null,score:{$avg:"$score"}}})

amount to :

SELECT AVG(score) from userInfo;

27、 Sum a field in the specified condition record in the table

db.userInfo.aggregate({$match:{createTime:{$gte:ISODate("2020-11-10T00:00:00Z"),$lt:ISODate("2020-11-11T00:00:00Z")}}},{$group:{_id:null,score:{$sum:"$score"}}})

amount to :

SELECT SUM(score) from userInfo where createTime >= '2020-11-10 00:00:00' and createTime < '2020-11-11 00:00:00';

28、 according to A surface , matching B Table all sets that meet the conditions , For example, according to the user table userInfo In the table userId Field find userAdress A collection of all addresses in the table , among userId Also for the userAdress In the field .

hypothesis Yes User set , Stored test data as follows :

db.userInfo.insert([
{ "_id" : 1, "userId" : "xxxx", "username" : "ruink", "website" : "www.51ste.com" },
{ "_id" : 2, "userId" : "yyyy", "username" : "foosingy", "website" : "www.scgossip.com" }
])
hypothesis Yes Address set , Stored test data as follows :

db.userAdress.insert([
{ "_id" : 1, "userId" : "xxxx", address: " Address of the test 1"},
{ "_id" : 2, "userId" : "yyyy", address: " Address of the test 2"},
{ "_id" : 3, "userId" : "xxxx", address: " Address of the test 3"},
])

Query statement :

db.userInfo.aggregate([
  {
    $lookup:
      {
        from: "userAdress",
        localField: "userId",
        foreignField: "userId",
        as: "address_detail"
      }
    },
  { $match : {"userId" :"xxxx"} }
])

The above table is to find userId="xxxx" A collection of all addresses of , The query results are as follows :

Copy

[
  {
    _id: 1,
    userId: 'xxxx',
    username: 'ruink',
    website:'www.51ste.com',
    address_docs: [
        {
          _id: 1,
          userId: 'xxxx',
          address: ' Address of the test 1'
        },
        {
          _id: 3,
          userId: 'xxxx',
          address: ' Address of the test 3'
        }
      ]
  }
]

Be careful :

What is the type of field , So what type of field value should be when querying , For example, if the field type is NumberLog, Then the query type should be executed when querying NumberLog, Such as

db.userInfo.find({id: NumberLog(10)})

from MongoDB Commonly used 28 Query statements

原网站

版权声明
本文为[Lucky little yellow man]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041105289937.html