1.返回指定条数的数据limit()
limit用来指定查询结果集数量上限,比如我们有100条数据,只想返回前20条,我们可以通过limit(20)来指定返回20条数据
ps:limit在小程序端默认及最大上限为20,在云函数端默认及最大上限为1000
具体写法:
//返回规定条数的数据
limit(){
wx.cloud.database().collection('goods')
.limit(3)
.get()
.then(res=>{
console.log('商品列表请求成功',res)
this.setData({
list:res.data
})
})
.catch(err=>{
console.error('商品列表请求失败',err)
})
}
2.分页方法skip()
limit()是限制只取几条数据,而skip()是跳过前面几条数据。经常两者配合实现分页效果。
(1)没有加skip()前获得的全部数据
(2)加了skip(1)后的效果
3.command数据库操作符
(1)gte()查询大于或等于指定值,gt()查询大于指定值
全部数据
使用gt(10)后
Page({
onLoad(){
let db=wx.cloud.database()
db.collection('goods')
.where({
price:db.command.gt(10),
})
.get()
.then(res=>{
console.log('成功',res)
})
.catch(err=>{
console.error('失败',err)
})
}
})
(2)lte()查询小于或等于指定值,lt()查询小于指定值
let db=wx.cloud.database()
db.collection('goods')
.where({
price:db.command.lt(10),
})
(3) 多种条件同时满足用and
//查询大于5小于10的所有商品
let db = wx.cloud.database()
const _ = db.command
db.collection('goods')
.where(_.and([{
price: _.gt(5)
},
{
price: _.lt(20)
}
]))