W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
查詢指令,用于表示邏輯 "與" 的關(guān)系,表示需同時(shí)滿足多個(gè)查詢篩選條件
示例代碼
如篩選出進(jìn)度大于 50 小于 100 的 todo:
流式寫法:
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
try {
return await db.collection('todo').where({
progress: _.gt(50).and(_.lt(100))
}).get()
} catch(e) {
console.error(e)
}
}
前置寫法:
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
try {
return await db.collection('todo').where({
memory: _.and(_.gt(50), _.lt(100))
}).get()
} catch(e) {
console.error(e)
}
}
查詢指令,用于表示邏輯 "或" 的關(guān)系,表示需同時(shí)滿足多個(gè)查詢篩選條件?;蛑噶钣袃煞N用法,一是可以進(jìn)行字段值的 “或” 操作,二是也可以進(jìn)行跨字段的 “或” 操作。
字段值的 “或” 操作指的是指定一個(gè)字段值為多個(gè)值之一即可:
字段值的或操作:示例代碼
如篩選出進(jìn)度大于 80 或小于 20 的 todo:
流式寫法:
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
try {
return await db.collection('todo').where({
progress: _.gt(80).or(_.lt(20))
}).get()
} catch(e) {
console.error(e)
}
}
前置寫法:
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
try {
return await db.collection('todo').where({
memory: _.or(_.gt(80), _.lt(20))
}).get()
} catch(e) {
console.error(e)
}
}
跨字段的 “或” 操作指條件 “或”,相當(dāng)于可以傳入多個(gè) where 語句,滿足其中一個(gè)即可,示例:
跨字段的或操作:示例代碼
如篩選出進(jìn)度大于 80 或已標(biāo)為已完成的 todo:
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
try {
return await db.collection('todo').where(_.or([
{
progress: _.gt(80)
},
{
done: true
}
]))
} catch(e) {
console.error(e)
}
}
支持端:小程序 2.8.3, 云函數(shù) 1.2.1, Web
查詢操作符,用于表示邏輯 "非" 的關(guān)系,表示需不滿足指定的條件。
如篩選出進(jìn)度不等于100的 todo:
const _ = db.command
db.collection('todo').where({
progress: _.not(_.eq(100))
})
not 也可搭配其他邏輯指令使用,包括 and, or, nor, not,如 or:
const _ = db.command
db.collection('todo').where({
progress: _.not(_.or([_.lt(50), _.eq(100)]))
})
支持端:小程序 2.8.3, 云函數(shù) 1.2.1, Web
查詢操作符,用于表示邏輯 "都不" 的關(guān)系,表示需不滿足指定的所有條件。如果記錄中沒有對(duì)應(yīng)的字段,則默認(rèn)滿足條件。
篩選出進(jìn)度既不小于20又不大于80的 todo :
const _ = db.command
db.collection('todo').where({
progress: _.nor([_.lt(20), _.gt(80)])
})
以上同時(shí)會(huì)篩選出不存在 progress 字段的記錄,如果要要求 progress 字段存在,可以用 exists 指令:
const _ = db.command
db.collection('todo').where({
progress: _.exists().nor([_.lt(20), _.gt(80)])
// 等價(jià)于以下非鏈?zhǔn)秸{(diào)用的寫法:
// progress: _.exists().and(_.nor([_.lt(20), _.gt(80)]))
}).get()
篩選出 progress 不小于 20 且 tags 數(shù)組不包含 miniprogram 字符串的記錄:
const _ = db.command
db.collection('todo').where(_.nor([{
progress: _.lt(20),
}, {
tags: 'miniprogram',
}])).get()
以上會(huì)篩選出滿足以下條件之一的記錄:
如果要求 progress 和 tags 字段存在,可以用 exists 指令:
const _ = db.command
db.collection('todo').where(
_.nor([{
progress: _.lt(20),
}, {
tags: 'miniprogram',
}])
.and({
progress: _.exists(true),
tags: _.exists(true),
})
)
方法接收兩種傳參方式,一是傳入一個(gè)數(shù)組參數(shù),二是傳入多個(gè)參數(shù),效果一樣。
// 傳入數(shù)組
function nor(expressions: Expression[]): Command
// 傳入多參數(shù)
function nor(...expressions: Expression[]): Command
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: