Mongodb common query statements (compared to SQL)

1. Query all records

db.mycol.find();
-- select * from mycol;

2. Query the records with name = "jack"

db.mycol.find({"name": "jack"});
-- select * from mycol where name= 'jack';

3. Query the records of the specified column name and age

db.mycol.find({}, {name: 1, age: 1});
-- select name, age from mycol;

4. Query the records with age >= 20

db.mycol.find({age: {$gte: 20}});
-- select * from mycol where age >= 20;

5. Fuzzy query (like)

db.mycol.find({name: /mongo/});
-- select * from mycol where name like '%mongo%';

db.mycol.find({name: /^mongo/});
-- select * from mycol where name like 'mongo%';

6. Paging query

db.mycol.find().limit(10).skip(5);
-- select * from mycol limit 5,10;

7. Sort 1 ascending order-1 descending order

db.mycol.find().sort({"_id": 1}); //升序
-- select * from mycol ORDER BY id ASC;

db.mycol.find().sort({"_id": -1}); //降序
-- select * from mycol ORDER BY id DESC;

Guess you like

Origin blog.csdn.net/icanlove/article/details/124315256