//普通数据查询
db.collection("china") //获取集合china的引用
.where({ //查询的条件操作符where
gdp: _.gt(3000) //查询筛选条件,gt表示字段需大于指定值。
})
.field({ //显示哪些字段
_id:false, //默认显示_id,这个隐藏
city: true,
province: true,
gdp:true
})
.orderBy('gdp', 'desc') //排序方式,降序排列
.skip(0) //跳过多少个记录(常用于分页),0表示这里不跳过
.limit(10) //限制显示多少条记录,这里为10,默认是20
.get() //获取根据查询条件筛选后的集合数据
.then(res => {
console.log(res.data)
})
.catch(err => {
console.error(err)
})
//聚合查询,大家写聚合操作的时候,可以参考这样的一个模板来写
db.collection('china').aggregate() //发起聚合操作
.match({ //类似于where,对记录进行筛选
gdp: _.gt(3000)
})
.project({ //类似于field,在这里可以新增字段
_id:false, //默认显示_id,这个隐藏
city: true,
province: true,
gdp:true
})
.sort({ //类似于orderBy
gdp: -1,
})
.skip(5) //类似于skip
.limit(1000) //类似于limit,不填默认是20,没有上限
.end() //注意,end标志聚合操作的完成
.then(res => console.log(res))
.catch(err => console.error(err))
云开发-聚合查询
最新推荐文章于 2022-10-26 11:12:59 发布