2021/7/26
以下所有的操作都对于集合User
- 匹配大于 小于
//查询大于20 小于50的文档
User.find({age: {$gt: 20, $lt: 50}}).then(result => console.log(result));
- 匹配包含
//查询爱好中包含**的文档
User.find({bobbies: {$in: ['爱好补充']}}).then(result => console.log(result));
- 选择要查询的字段
//查询name 和 email字段,不想查询的字段前面加上‘-’
User.find().select('name email -_id').then(result => console.log(result));
- 将数据库按照年龄进行排序
//按年龄升序排序
User.find().sort('age').then(result => console.log(result));
//按年龄聆排序
User.find().sort('-age').then(result => console.log(result));
- skip 跳过多少条数据 limit 限制查询数量
//跳过前两条数据, 查询之后最前的两条文档
User.find().skip(2).limit(2).then(result => console.log(result));