查詢指令之比較和邏輯操作符
- gt 大于
- lt 小于
- eq 等于
- neq 不等于
- lte 小于或等于
- gte 大于或等于
- in 在數(shù)組中
- nin 不在數(shù)組中
- and 條件與
- or 條件或
- not 條件非
- nor 都不
示例:查詢廣東省內(nèi)角骤、GDP 在 3000 億以上且在 1 萬(wàn)億以下的城市。
.where({
province:_.eq("廣東"),
gdp:_.gt(3000).and(_.lt(10000))
})
構(gòu)建查詢條件的5個(gè)方法
- Collection.where() 查詢的條件指令
- Collection.field() 顯示哪些字段
- Collection.orderBy() 排序方式微王, orderBy('字段名', '排序方式') desc 降序羊精、asc 升序
- Collection.skip() 跳過(guò)多少條記錄(常用于分頁(yè))替裆,0不跳過(guò)体谒;每頁(yè)20個(gè):skip((n-1)*20)
- Collection.limit() 限制顯示多少條記錄(最大數(shù)量:小程序端20,服務(wù)端100)
示例:
const db = wx.cloud.database() //獲取數(shù)據(jù)庫(kù)的引用
const _ = db.command //獲取數(shù)據(jù)庫(kù)查詢及更新指令
db.collection("china") //獲取集合china的引用
.where({ //查詢的條件指令where
gdp: _.gt(3000) //查詢篩選條件翻默,gt表示字段需大于指定值缸沃。
})
.field({ //顯示哪些字段
_id:false, //默認(rèn)顯示_id,這個(gè)隱藏
city: true,
province: true,
gdp:true
})
.orderBy('gdp', 'desc') //排序方式修械,降序排列
.skip(0) //跳過(guò)多少個(gè)記錄(常用于分頁(yè))趾牧,0表示這里不跳過(guò)
.limit(10) //限制顯示多少條記錄,這里為10
.get() //獲取根據(jù)查詢條件篩選后的集合數(shù)據(jù)
.then(res => {
console.log(res.data)
})
.catch(err => {
console.error(err)
})
大家可以留意一下數(shù)據(jù)查詢的鏈?zhǔn)綄懛ǎ?wx.cloud.database().collection('數(shù)據(jù)庫(kù)名').where().get().then().catch()肯污,前半部分是數(shù)據(jù)查詢時(shí)對(duì)對(duì)象的引用和方法的調(diào)用翘单;后半部分是Promise對(duì)象的方法,Promise對(duì)象是get的返回值蹦渣。寫的時(shí)候?yàn)榱俗尳Y(jié)構(gòu)更加清晰哄芜,我們做了換行處理,寫在同一行也是可以的剂桥。
正則匹配
options參數(shù):options 支持 i, m, s 這三個(gè) flag忠烛。
- i 大小寫不敏感
- m 跨行匹配
- s 讓
.
可以匹配包括換行符在內(nèi)的所有字符
示例:
// 數(shù)據(jù)庫(kù)正則對(duì)象
db.collection('todos').where({
description: db.RegExp({
regexp: 'miniprogram',
options: 'i',
})
})
數(shù)據(jù)庫(kù)常用操作
Collection.add() 新增記錄
db.collection('todos').add({
data:{
description:'待辦測(cè)試',
due:'2021-01-20',
tags:['clound','database'],
style:{
color:'red'
},
done:false
},
}).then(res=>{
console.log(res)
}).catch(console.err)
Collection.get() 查詢記錄
//查詢多條記錄
db.collection('todos').where({
'style.color':'red'
}).get()
//查詢單條記錄
db.collection('todos').doc('28ee4e3e600699ab001af1fc07ccd456').get().then(res=>{
console.log('獲取數(shù)據(jù):',res.data);
})
Collection.remove() 刪除記錄
僅可刪除創(chuàng)建者創(chuàng)建的數(shù)據(jù)
db.collection('todos').doc('79550af26006a5df001966cd11ad4763').remove().then(res=>{
console.log(res);
});
Collection.update() 更新記錄
僅可修改創(chuàng)建者創(chuàng)建的數(shù)據(jù)
db.collection('todos').doc('1526e12a6006a1e100148dbc1cf7bb7a').update({
data:{
'done':false,
'style.color':'red'
}
}).then(res=>{
console.log(res);
});
Collection.count() 統(tǒng)計(jì)記錄
小程序端:僅能統(tǒng)計(jì)有讀權(quán)限的記錄數(shù)属提;
管理端:可以統(tǒng)計(jì)集合的所有記錄數(shù)权逗。
- 注意:與集合權(quán)限設(shè)置有關(guān)
field美尸、orderBy、skip斟薇、limit 對(duì) count 是無(wú)效的师坎,只有 where 才會(huì)影響 count 的結(jié)果,count 只會(huì)返回記錄數(shù)堪滨,不會(huì)返回查詢到的數(shù)據(jù)胯陋。
示例:
const db = wx.cloud.database()
const _ = db.command
db.collection("china")
.where({
gdp: _.gt(3000)
})
.count().then(res => {
console.log(res.total)
})
云函數(shù)操作數(shù)據(jù)庫(kù)
- 注意:
cloud.database() wx.cloud.database()
云函數(shù)端的數(shù)據(jù)庫(kù)引用和小程序端有所不同
新建一個(gè)云函數(shù)todosdata,然后輸入以下代碼:
// 云函數(shù)入口函數(shù)
exports.main = async (event, context) => {
const db=cloud.database();
const _=db.command;
return await db.collection('todos').where({
'done':false
}).get();
}
然后右鍵todosdata云函數(shù)根目錄選擇在終端打開(kāi)袱箱,輸入npm install遏乔,之后“上傳并部署所有文件”。
小程序調(diào)用云函數(shù):
todosBtn(){
wx.cloud.callFunction({
name:'todosdata'
}).then(res=>{
console.log('todos云函數(shù):',res.result.data);
})
}
在控制臺(tái)可以查看到查詢結(jié)果发笔。