MongoDB query command details, MongoDB command details

Source: Internet
Author: User
Tags mongodb query

MongoDB query command details, MongoDB command details

1. query all records

Copy the Code as follows:

db.userInfo.find();

Equivalent:Select * from userInfo;

By default, 20 records are displayed on each page. If no record is displayed, you can use the it iteration command to query data on the next page. Note: Type It commandCannot contain ";"

However, you can set the size of data displayed on each page.DBQuery. shellBatchSize = 50;In this way, 50 records are displayed on each page.


2. query duplicate data of a column in the current clustering set after Removal

Copy the Code as follows:

db.userInfo.distinct("name");

The same data in name is filtered out.

Equivalent: Select distict name from userInfo;

3. query records with age = 22

Copy the Code as follows:

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

Equivalent:Select * from userInfo where age = 22;



4. query records of age> 22

Copy the Code as follows:

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

Equivalent:Select * from userInfo where age> 22;



5. query records of age <22

Copy the Code as follows:

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

Equivalent:Select * from userInfo where age <22;



6. query records with age> = 25
Copy the Code as follows:
db.userInfo.find({age: {$gte: 25}});

Equivalent:Select * from userInfo where age> = 25;



7. query records of age <= 25
Copy the Code as follows:
db.userInfo.find({age: {$lte: 25}});


8. query age> = 23 and age <= 26

Copy the Code as follows:
db.userInfo.find({age: {$gte: 23, $lte: 26}});



9. query data whose name contains mongo
Copy the Code as follows:
db.userInfo.find({name: /mongo/});

// Equivalent to %

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



10. query names starting with mongo
Copy the Code as follows:
db.userInfo.find({name: /^mongo/});
Select * from userInfo where name like 'mongo % ';


11. query the name and age data of a specified Column

Copy the Code as follows:
db.userInfo.find({}, {name: 1, age: 1});
Equivalent: Select name, age from userInfo;
Of course, "true" or "false" can be used for name. If "true" is used, "river name: 1" has the same effect. If "false" is used, "name" is excluded and column information other than "name" is displayed.


12. query the name and age data of a specified column. age> 25

Copy the Code as follows:
db.userInfo.find({age: {$gt: 25}}, {name: 1, age: 1});
Equivalent: Select name, age from userInfo where age> 25;


13. sort by age

Copy the Code as follows:

Ascending Order:

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

Descending order:

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


14. query data with name = zhangsan and age = 22
Copy the Code as follows:
db.userInfo.find({name: 'zhangsan', age: 22});
Equivalent: Select * from userInfo where name = 'hangsan' and age = '22 ';


15. query the first five data entries

Copy the Code as follows:
db.userInfo.find().limit(5);
Equivalent: Select top 5 * from userInfo;


16. Query 10 data records later

Copy the Code as follows:
db.userInfo.find().skip(10);

Equivalent:

Select * from userInfo where id not in (

Select top 10 * from userInfo
);



17. query data between 5-10

Copy the Code as follows:
db.userInfo.find().limit(10).skip(5);
Limit is the limit on the displayed quantity, and skip is the number of skipped.


18. or and query

Copy the Code as follows:
db.userInfo.find({$or: [{age: 22}, {age: 25}]});
Equivalent: Select * from userInfo where age = 22 or age = 25;


19. query the first data entry.

Copy the Code as follows:
db.userInfo.findOne();

Equivalent:

Select top 1 * from userInfo;

Db. userInfo. find (). limit (1 );



20. query the number of records in a result set
Copy the Code as follows:
db.userInfo.find({age: {$gte: 25}}).count();

Equivalent:Select count (*) from userInfo where age> = 20;



21. sort by Column
Copy the Code as follows:
db.userInfo.find({sex: {$exists: true}}).count();
Equivalent: Select count (sex) from userInfo;


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.