Mongodb常用查询语句(对比SQL)

1、查询所有记录

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

2、查询 name = "jack" 的记录

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

3、查询指定列 name、age 的记录

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

4、查询 age >= 20 的记录

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

5、模糊查询(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、分页查询

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

7、排序 1 升序 -1 降序

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;

猜你喜欢

转载自blog.csdn.net/icanlove/article/details/124315256