1. Check all records
Copy the code code as follows:
Db.userInfo.find ();
Equivalent:select* from UserInfo;
The default is 20 records per page, and you can query the next page of data with the IT iteration command when the display is not displayed. Note: Type the
it command cannot take ";"
But you can set the size of the data displayed on each page, using dbquery.shellbatchsize=; This results in 50 records per page.
2. Duplicate data for a column in the current clustered collection after the query is removed
Copy the code code as follows:
Db.userInfo.distinct ("name");
Will filter out the same data in name
equivalent:
select Distict name from UserInfo;
3. Check the record of age = 22
Copy the code code as follows:
Db.userInfo.find ({"Age": 22});
Equivalent to: select * from userInfo where age =;
4. Check the records of age > 22
Copy the code code as follows:
Db.userInfo.find ({age: {$gt: 22}});
Equivalent to:select * from UserInfo where >22;
5. Check the records of age < 22
Copy the code code as follows:
Db.userInfo.find ({age: {$lt: 22}});
Equivalent to:select * from UserInfo where <22;
6. Check the records of age >= 25
Copy the code code as follows:
Db.userInfo.find ({age: {$gte: 25}});
Equivalent to:select * from UserInfo where age >=;
7. Check the records of age <= 25
Copy the code code as follows:
Db.userInfo.find ({age: {$lte: 25}});
8. Query age >= 23 and age <= 26
Copy the code code as follows:
Db.userInfo.find ({age: {$gte: $lte: 26}});
9. Query the data containing MONGO in name
Copy the code code as follows:
Db.userInfo.find ({name:/mongo/});
Equal to percent
SELECT * from UserInfo where name is like '%mongo% ';
10, query the name in the beginning of MONGO
Copy the code code as follows:
Db.userInfo.find ({name:/^mongo/});
SELECT * from UserInfo where name is like ' mongo% ';
11. Query the specified column name, age data
Copy the code code as follows:
Db.userInfo.find ({}, {name:1, age:1});
Equivalent:
select name, age from UserInfo;
Of course name can also be used with true or false, as in the case of Ture River Name:1 effect, if False is to exclude name, display column information other than name.
12. Query the specified column name, age data, age > 25
Copy the code code as follows:
Db.userInfo.find ({age: {$gt: +}}, {name:1, age:1});
Equivalent:
select name, age from UserInfo where >25;
13. Sort by age
Copy the code code as follows:
Ascending:
Db.userInfo.find (). Sort ({age:1});
Descending:
Db.userInfo.find (). Sort ({Age:-1});
14. Query name = Zhangsan, age = 22 data
Copy the code code as follows:
Db.userInfo.find ({name: ' Zhangsan ', age:22});
Equivalent:
SELECT * from userInfo where name = ' Zhangsan ' and "age = '";
15, query the first 5 data
Copy the code code as follows:
Db.userInfo.find (). Limit (5);
Equivalent:
Select Top 5 * from UserInfo;
16, query 10 after the data
Copy the code code as follows:
Db.userInfo.find (). Skip (10);
Equivalent:
SELECT * from UserInfo where ID not in (
Select Top Ten * from UserInfo
);
17, query the data between 5-10
Copy the code code as follows:
Db.userInfo.find (). Limit (Ten). Skip (5);
Limit is the number of limits displayed, and Skip is the number skipped.
18, OR and query
Copy the code code as follows:
Db.userInfo.find ({$or: [{age:22}, {age:25}]});
Equivalent:
SELECT * from userInfo where age = n or age =;
19. Query the first piece of data
Copy the code code as follows:
Db.userInfo.findOne ();
Equivalent:
Select top 1 * from UserInfo;
Db.userInfo.find (). limit (1);
20. Query the number of record bars for a result set
Copy the code code as follows:
Db.userInfo.find ({age: {$gte: +}}). Count ();
Equivalent to:select COUNT (*) from userInfo where age >=;
21. Sort by a column
Copy the code code as follows:
Db.userInfo.find ({sex: {$exists: true}}). Count ();
equivalent to:
select count (Sex) from UserInfo;
MongoDB Query Command detailed