新聞中心
db.command.and
查詢指令,用于表示邏輯 "與" 的關系,表示需同時滿足多個查詢篩選條件

創(chuàng)新互聯公司是一家以網站建設公司、網頁設計、品牌設計、軟件運維、成都網站推廣、小程序App開發(fā)等移動開發(fā)為一體互聯網公司。已累計為成都OPP膠袋等眾行業(yè)中小客戶提供優(yōu)質的互聯網建站和軟件開發(fā)服務。
示例代碼
如篩選出進度大于 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)
}
}
db.command.or
查詢指令,用于表示邏輯 "或" 的關系,表示需同時滿足多個查詢篩選條件?;蛑噶钣袃煞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)
}
}
跨字段的 “或” 操作指條件 “或”,相當于可以傳入多個 where 語句,滿足其中一個即可,示例:
跨字段的或操作:示例代碼
如篩選出進度大于 80 或已標為已完成的 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)
}
}
db.command.not
支持端:小程序 2.8.3, 云函數 1.2.1, Web
查詢操作符,用于表示邏輯 "非" 的關系,表示需不滿足指定的條件。
參數
command: Command
返回值
Command
示例
如篩選出進度不等于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)]))
})
db.command.nor
支持端:小程序 2.8.3, 云函數 1.2.1, Web
查詢操作符,用于表示邏輯 "都不" 的關系,表示需不滿足指定的所有條件。如果記錄中沒有對應的字段,則默認滿足條件。
參數
expressions: any[]
返回值
Command
示例 1
篩選出進度既不小于20又不大于80的 todo :
const _ = db.command
db.collection('todo').where({
progress: _.nor([_.lt(20), _.gt(80)])
})
以上同時會篩選出不存在 progress 字段的記錄,如果要要求 progress 字段存在,可以用 exists 指令:
const _ = db.command
db.collection('todo').where({
progress: _.exists().nor([_.lt(20), _.gt(80)])
// 等價于以下非鏈式調用的寫法:
// progress: _.exists().and(_.nor([_.lt(20), _.gt(80)]))
}).get()
示例 2
篩選出 progress 不小于 20 且 tags 數組不包含 miniprogram 字符串的記錄:
const _ = db.command
db.collection('todo').where(_.nor([{
progress: _.lt(20),
}, {
tags: 'miniprogram',
}])).get()
以上會篩選出滿足以下條件之一的記錄:
- progress 不小于 20 且 tags 數組不包含 miniprogram 字符串
- progress 不小于 20 且 tags 字段不存在
- progress 字段不存在 且 tags 數組不包含 miniprogram 字符串
- progress 不小于 20 且 tags 字段不存在
如果要求 progress 和 tags 字段存在,可以用 exists 指令:
const _ = db.command
db.collection('todo').where(
_.nor([{
progress: _.lt(20),
}, {
tags: 'miniprogram',
}])
.and({
progress: _.exists(true),
tags: _.exists(true),
})
)
調用風格
方法接收兩種傳參方式,一是傳入一個數組參數,二是傳入多個參數,效果一樣。
// 傳入數組
function nor(expressions: Expression[]): Command
// 傳入多參數
function nor(...expressions: Expression[]): Command
分享名稱:創(chuàng)新互聯小程序教程:微信小程序云開發(fā)服務端數據庫API查詢指令
當前網址:http://www.5511xx.com/article/dpggghj.html


咨詢
建站咨詢
