W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
支持端:小程序 2.8.3, 云函數(shù) 1.2.1, Web
數(shù)組更新操作符。對(duì)一個(gè)值為數(shù)組的字段,往數(shù)組添加一個(gè)或多個(gè)值?;蜃侄卧瓰榭?,則創(chuàng)建該字段并設(shè)數(shù)組為傳入值。
屬性 | 類型 | 默認(rèn)值 | 必填 | 說明 |
---|---|---|---|---|
each | Array.<any> | 是 | 要插入的所有元素 | |
position | number | 否 | 從哪個(gè)位置開始插入,不填則是尾部 | |
sort | number | 否 | 對(duì)結(jié)果數(shù)組排序 | |
slice | number | 否 | 限制結(jié)果數(shù)組長(zhǎng)度 |
要求必須同時(shí)有 each 參數(shù)存在。
非負(fù)數(shù)代表從數(shù)組開始位置數(shù)的位置,從 0 開始計(jì)。如果數(shù)值大于等于數(shù)組長(zhǎng)度,則視為在尾部添加。負(fù)數(shù)代表從數(shù)組尾部倒數(shù)的位置,比如 -1 就代表倒數(shù)第二個(gè)元素的位置。如果負(fù)數(shù)數(shù)值的絕對(duì)值大于等于數(shù)組長(zhǎng)度,則視為從數(shù)組頭部添加。
要求必須同時(shí)有 each 參數(shù)存在。給定 1 代表升序,-1 代表降序。
如果數(shù)組元素是記錄,則用 { <字段>: 1 | -1 } 的格式表示根據(jù)記錄中的什么字段做升降序排序。
要求必須同時(shí)有 each 參數(shù)存在
值 | 說明 |
---|---|
0 | 將字段更新為空數(shù)組 |
正數(shù) | 數(shù)組只保留前 n 個(gè)元素 |
負(fù)數(shù) | 數(shù)組只保留后 n 個(gè)元素 |
以上定義是從小程序 2.8.3 / 云函數(shù) SDK 1.2.1 起支持,對(duì)于之前的版本,使用的是如下函數(shù)簽名,新版中對(duì)舊版簽名有兼容。
舊版簽名:傳入一個(gè)數(shù)組,該數(shù)組的每個(gè)元素會(huì)被追加到字段數(shù)組的尾部
function push(values: any[]): Command
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push(['mini-program', 'cloud'])
}
})
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: ['mini-program', 'cloud'],
position: 1,
})
}
})
插入后對(duì)整個(gè)數(shù)組做排序
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: ['mini-program', 'cloud'],
sort: 1,
})
}
})
不插入,只對(duì)數(shù)組做排序
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: [],
sort: 1,
})
}
})
如果字段是對(duì)象數(shù)組,可以如下根據(jù)元素對(duì)象里的字段進(jìn)行排序:
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: [
{ name: 'miniprogram', weight: 8 },
{ name: 'cloud', weight: 6 },
],
sort: {
weight: 1,
},
})
}
})
插入后只保留后 2 個(gè)元素
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: ['mini-program', 'cloud'],
slice: -2,
})
}
})
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: ['mini-program', 'cloud'],
position: 1,
slice: 2,
sort: 1,
})
}
})
支持端:小程序 , 云函數(shù) , Web
數(shù)組更新操作符,對(duì)一個(gè)值為數(shù)組的字段,將數(shù)組尾部元素刪除
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.pop()
}
})
支持端:小程序 , 云函數(shù) , Web
數(shù)組更新操作符,對(duì)一個(gè)值為數(shù)組的字段,往數(shù)組頭部添加一個(gè)或多個(gè)值?;蜃侄卧瓰榭眨瑒t創(chuàng)建該字段并設(shè)數(shù)組為傳入值。
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.unshift(['mini-program', 'cloud'])
}
})
支持端:小程序 , 云函數(shù) , Web
數(shù)組更新操作符,對(duì)一個(gè)值為數(shù)組的字段,將數(shù)組頭部元素刪除。
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.shift()
}
})
支持端:小程序 2.8.3, 云函數(shù) 1.2.1, Web
數(shù)組更新操作符。給定一個(gè)值或一個(gè)查詢條件,將數(shù)組中所有匹配給定值或查詢條件的元素都移除掉。
值或查詢條件
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.pull('database')
}
})
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.pull(_.in(['database', 'cloud']))
}
})
假設(shè)有字段 places 數(shù)組中的元素結(jié)構(gòu)如下
{
"type": string
"area": number
"age": number
}
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
places: _.pull({
area: _.gt(100),
age: _.lt(2),
})
}
})
假設(shè)有字段 cities 數(shù)組中的元素結(jié)構(gòu)如下
{
"name": string
"places": Place[]
}
Place 結(jié)構(gòu)如下:
{
"type": string
"area": number
"age": number
}
可用 elemMatch 匹配嵌套在對(duì)象數(shù)組里面的對(duì)象數(shù)組字段 places
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
cities: _.pull({
places: _.elemMatch({
area: _.gt(100),
age: _.lt(2),
})
})
}
})
支持端:小程序 2.8.3, 云函數(shù) 1.2.1, Web
數(shù)組更新操作符。給定一個(gè)值或一個(gè)查詢條件,將數(shù)組中所有匹配給定值的元素都移除掉。跟 pull 的差別在于只能指定常量值、傳入的是數(shù)組。
值或查詢條件
從 tags 中移除所有 database 和 cloud 字符串
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.pullAll(['database', 'cloud'])
}
})
支持端:小程序 2.8.3, 云函數(shù) 1.2.1, Web
數(shù)組更新操作符。原子操作。給定一個(gè)或多個(gè)元素,除非數(shù)組中已存在該元素,否則添加進(jìn)數(shù)組。
要添加進(jìn)數(shù)組的一個(gè)或多個(gè)元素
屬性 | 類型 | 默認(rèn)值 | 必填 | 說明 |
---|---|---|---|---|
each | Array.<any> | 是 | 數(shù)組,用于同時(shí)指定多個(gè)要插入數(shù)組的元素 |
如果 tags 數(shù)組中不包含 database,添加進(jìn)去
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.addToSet('database')
}
})
需傳入一個(gè)對(duì)象,其中有一個(gè)字段 each,其值為數(shù)組,每個(gè)元素就是要添加的元素
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.addToSet({
each: ['database', 'cloud']
})
}
})
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)系方式:
更多建議: