新聞中心
Command.all(values: any[]): Command
支持端:小程序 2.8.3, 云函數(shù) 1.2.1, Web
站在用戶的角度思考問題,與客戶深入溝通,找到大安網(wǎng)站設(shè)計(jì)與大安網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站建設(shè)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊(cè)、雅安服務(wù)器托管、企業(yè)郵箱。業(yè)務(wù)覆蓋大安地區(qū)。
數(shù)組查詢操作符。用于數(shù)組字段的查詢篩選條件,要求數(shù)組字段中包含給定數(shù)組的所有元素。
參數(shù)
values: any[]
返回值
Command
示例代碼 1:普通數(shù)組
找出 tags 數(shù)組字段同時(shí)包含 cloud 和 database 的記錄
const _ = db.command
db.collection('todos').where({
tags: _.all(['cloud', 'database'])
})
.get({
success: console.log,
fail: console.error
})
示例代碼 2:對(duì)象數(shù)組
如果數(shù)組元素是對(duì)象,則可以用 _.elemMatch 匹配對(duì)象的部分字段
假設(shè)有字段 places 定義如下:
{
"type": string
"area": number
"age": number
}
找出數(shù)組字段中至少同時(shí)包含一個(gè)滿足 “area 大于 100 且 age 小于 2” 的元素和一個(gè)滿足 “type 為 mall 且 age 大于 5” 的元素
const _ = db.command
db.collection('todos').where({
places: _.all([
_.elemMatch({
area: _.gt(100),
age: _.lt(2),
}),
_.elemMatch({
name: 'mall',
age: _.gt(5),
}),
]),
})
.get({
success: console.log,
fail: console.error,
})
Command.elemMatch(condition: Object|Command): Command
支持端:小程序 2.8.3, 云函數(shù) 1.2.1, Web
用于數(shù)組字段的查詢篩選條件,要求數(shù)組中包含至少一個(gè)滿足 elemMatch 給定的所有條件的元素
參數(shù)
condition: Object|Command
匹配條件
返回值
Command
示例代碼:數(shù)組是對(duì)象數(shù)組的情況
假設(shè)集合示例數(shù)據(jù)如下:
{
"_id": "a0",
"city": "x0",
"places": [{
"type": "garden",
"area": 300,
"age": 1
}, {
"type": "theatre",
"area": 50,
"age": 15
}]
}
找出 places 數(shù)組字段中至少同時(shí)包含一個(gè)滿足 “area 大于 100 且 age 小于 2” 的元素
const _ = db.command
db.collection('todos').where({
places: _.elemMatch({
area: _.gt(100),
age: _.lt(2),
})
})
.get()
注意*:如果不使用 elemMatch 而直接如下指定條件,則表示的是 places 數(shù)組字段中至少有一個(gè)元素的 area 字段大于 100 且 places 數(shù)組字段中至少有一個(gè)元素的 age 字段小于 2:
const _ = db.command
db.collection('todos').where({
places: {
area: _.gt(100),
age: _.lt(2),
}
})
.get()
示例代碼:數(shù)組元素都是普通數(shù)據(jù)類型的情況
假設(shè)集合示例數(shù)據(jù)如下:
{
"_id": "a0",
"scores": [60, 80, 90]
}
找出 scores 數(shù)組字段中至少同時(shí)包含一個(gè)滿足 “大于 80 且小于 100” 的元素
const _ = db.command
db.collection('todos').where({
places: _.elemMatch(_.gt(80).lt(100))
})
.get()
Command.size(value: string): Command
支持端:小程序 2.8.3, 云函數(shù) 1.2.1, Web
更新操作符,用于數(shù)組字段的查詢篩選條件,要求數(shù)組長(zhǎng)度為給定值
參數(shù)
value: string
返回值
Command
示例
找出 tags 數(shù)組字段長(zhǎng)度為 2 的所有記錄
const _ = db.command
db.collection('todos').where({
places: _.size(2)
})
.get({
success: console.log,
fail: console.error,
})
新聞標(biāo)題:創(chuàng)新互聯(lián)小程序教程:SDK數(shù)據(jù)庫Command·查詢·數(shù)組操作符
分享網(wǎng)址:http://www.dlmjj.cn/article/dpsdipo.html


咨詢
建站咨詢

