新聞中心
AggregateCommand.arrayElemAt(value: Expression[]): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
在金平等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營銷,外貿(mào)網(wǎng)站建設(shè),金平網(wǎng)站建設(shè)費(fèi)用合理。
聚合操作符。返回在指定數(shù)組下標(biāo)的元素。
參數(shù)
value: Expression[]
[
返回值
Object
API 說明
語法如下:
db.command.aggregate.arrayElemAt([, ])
示例代碼
假設(shè)集合 exams 有如下記錄:
{ "_id": 1, "scores": [80, 60, 65, 90] }
{ "_id": 2, "scores": [78] }
{ "_id": 3, "scores": [95, 88, 92] }
求各個(gè)第一次考試的分?jǐn)?shù)和和最后一次的分?jǐn)?shù):
const $ = db.command.aggregate
db.collection('exams').aggregate()
.project({
first: $.arrayElemAt(['$scores', 0]),
last: $.arrayElemAt(['$scores', -1]),
})
.end()
返回結(jié)果如下:
{ "_id": 1, "first": 80, "last": 90 }
{ "_id": 2, "first": 78, "last": 78 }
{ "_id": 3, "first": 95, "last": 92 }AggregateCommand.arrayToObject(value: any): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。將一個(gè)數(shù)組轉(zhuǎn)換為對象。
參數(shù)
value: any
返回值
Object
API 說明
語法可以取兩種:
第一種:傳入一個(gè)二維數(shù)組,第二維的數(shù)組長度必須為 2,其第一個(gè)值為字段名,第二個(gè)值為字段值
db.command.aggregate.arrayToObject([
[, ],
[, ],
...
])
第二種:傳入一個(gè)對象數(shù)組,各個(gè)對象必須包含字段 k 和 v,分別指定字段名和字段值
db.command.aggregate.arrayToObject([
{ "k": , "v": },
{ "k": , "v": },
...
])
傳入 arrayToObject 的參數(shù)只要可以解析為上述兩種表示法之一即可。
示例代碼
假設(shè)集合 shops 有如下記錄:
{ "_id": 1, "sales": [ ["max", 100], ["min", 50] ] }
{ "_id": 2, "sales": [ ["max", 70], ["min", 60] ] }
{ "_id": 3, "sales": [ { "k": "max", "v": 50 }, { "k": "min", "v": 30 } ] }
求各個(gè)第一次考試的分?jǐn)?shù)和和最后一次的分?jǐn)?shù):
const $ = db.command.aggregate
db.collection('shops').aggregate()
.project({
sales: $.arrayToObject('$sales'),
})
.end()
返回結(jié)果如下:
{ "_id": 1, "sales": { "max": 100, "min": 50 } }
{ "_id": 2, "sales": { "max": 70, "min": 60 } }
{ "_id": 3, "sales": { "max": 50, "min": 30 } }AggregateCommand.concatArrays(value: Expression[]): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。將多個(gè)數(shù)組拼接成一個(gè)數(shù)組。
參數(shù)
value: Expression[]
[
返回值
Object
API 說明
語法如下:
db.command.aggregate.arrayToObject([ , , ... ])
參數(shù)可以是任意解析為數(shù)組的表達(dá)式。
示例代碼
假設(shè)集合 items 有如下記錄:
{ "_id": 1, "fruits": [ "apple" ], "vegetables": [ "carrot" ] }
{ "_id": 2, "fruits": [ "orange", "lemon" ], "vegetables": [ "cabbage" ] }
{ "_id": 3, "fruits": [ "strawberry" ], "vegetables": [ "spinach" ] }
const $ = db.command.aggregate
db.collection('items').aggregate()
.project({
list: $.concatArrays(['$fruits', '$vegetables']),
})
.end()
返回結(jié)果如下:
{ "_id": 1, "list": [ "apple", "carrot" ] }
{ "_id": 2, "list": [ "orange", "lemon", "cabbage" ] }
{ "_id": 3, "list": [ "strawberry", "spinach" ] }AggregateCommand.filter(value: any): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。根據(jù)給定條件返回滿足條件的數(shù)組的子集。
參數(shù)
value: any
返回值
Object
API 說明
語法如下:
db.command.aggregate.filter({
input: ,
as: ,
cond:
})
| 字段 | 說明 |
|---|---|
| input | 一個(gè)可以解析為數(shù)組的表達(dá)式 |
| as | 可選,用于表示數(shù)組各個(gè)元素的變量,默認(rèn)為 this |
| cond | 一個(gè)可以解析為布爾值的表達(dá)式,用于判斷各個(gè)元素是否滿足條件,各個(gè)元素的名字由 as 參數(shù)決定(參數(shù)名需加 $$ 前綴,如 $$this) |
參數(shù)可以是任意解析為數(shù)組的表達(dá)式。
示例代碼
假設(shè)集合 fruits 有如下記錄:
{
"_id": 1,
"stock": [
{ "name": "apple", "price": 10 },
{ "name": "orange", "price": 20 }
],
}
{
"_id": 2,
"stock": [
{ "name": "lemon", "price": 15 },
],
}
const _ = db.command
const $ = db.command.aggregate
db.collection('fruits').aggregate()
.project({
stock: $.filter({
input: '$stock',
as: 'item',
cond: $.gte(['$$item.price', 15])
})
})
.end()
返回結(jié)果如下:
{ "_id": 1, "stock": [ { "name": "orange", "price": 20} ] }
{ "_id": 2, "stock": [ { "name": "lemon", "price": 15 } ] }AggregateCommand.in(value: Expression[]): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。給定一個(gè)值和一個(gè)數(shù)組,如果值在數(shù)組中則返回 true,否則返回 false。
參數(shù)
value: Expression[]
[
返回值
Object
API 說明
語法如下:
db.command.aggregate.in([, ])
示例代碼
假設(shè)集合 shops 有如下記錄:
{ "_id": 1, "topsellers": ["bread", "ice cream", "butter"] }
{ "_id": 2, "topsellers": ["ice cream", "cheese", "yagurt"] }
{ "_id": 3, "topsellers": ["croissant", "cucumber", "coconut"] }
標(biāo)記銷量最高的商品包含 ice cream 的記錄。
const $ = db.command.aggregate
db.collection('price').aggregate()
.project({
included: $.in(['ice cream', '$topsellers'])
})
.end()
返回結(jié)果如下:
{ "_id": 1, "included": true }
{ "_id": 2, "included": true }
{ "_id": 3, "included": false }AggregateCommand.indexOfArray(value: Expression[]): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。在數(shù)組中找出等于給定值的第一個(gè)元素的下標(biāo),如果找不到則返回 -1。
參數(shù)
value: Expression[]
[
返回值
Object
API 說明
語法如下:
db.command.aggregate.indexOfArray([ , , , ])
| 字段 | 類型 | 說明 |
|---|---|---|
| string | 一個(gè)可以解析為數(shù)組的表達(dá)式,如果解析為 null,則 indexOfArray 返回 null |
| string | 對數(shù)據(jù)各個(gè)元素應(yīng)用的條件匹配表達(dá)式 |
| integer | 可選,用于指定搜索的開始下標(biāo),必須是非負(fù)整數(shù) |
| integer | 可選,用于指定搜索的結(jié)束下標(biāo),必須是非負(fù)整數(shù),指定了 時(shí)也應(yīng)指定 ,否則 默認(rèn)當(dāng)做 |
參數(shù)可以是任意解析為數(shù)組的表達(dá)式。
示例代碼
假設(shè)集合 stats 有如下記錄:
{
"_id": 1,
"sales": [ 1, 6, 2, 2, 5 ]
}
{
"_id": 2,
"sales": [ 4, 2, 1, 5, 2 ]
}
{
"_id": 3,
"sales": [ 2, 5, 3, 3, 1 ]
}
const $ = db.command.aggregate
db.collection('stats').aggregate()
.project({
index: $.indexOfArray(['$sales', 2, 2])
})
.end()
返回結(jié)果如下:
{ "_id": 1, "index": 2 }
{ "_id": 2, "index": 4 }
{ "_id": 3, "index": -1 }AggregateCommand.isArray(value: Expression): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。判斷給定表達(dá)式是否是數(shù)組,返回布爾值。
參數(shù)
value: Expression
表達(dá)式
返回值
Object
API 說明
語法如下:
db.command.aggregate.isArray()
參數(shù)可以是任意表達(dá)式。
示例代碼
假設(shè)集合 stats 有如下記錄:
{
"_id": 1,
"base": 10,
"sales": [ 1, 6, 2, 2, 5 ]
}
{
"_id": 2,
"base": 1,
"sales": 100
}
計(jì)算總銷量,如果 sales 是數(shù)字,則求 sales * base,如果 sales 是數(shù)組,則求數(shù)組元素之和與 base 的乘積。
const $ = db.command.aggregate
db.collection('stats').aggregate()
.project({
sum: $.cond({
if: $.isArray('$sales'),
then: $.multiply([$.sum(['$sales']), '$base']),
else: $.multiply(['$sales', '$base']),
})
})
.end()
返回結(jié)果如下:
{ "_id": 1, "index": 160 }
{ "_id": 2, "index": 100 }AggregateCommand.map(value: any): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。類似 JavaScript Array 上的 map 方法,將給定數(shù)組的每個(gè)元素按給定轉(zhuǎn)換方法轉(zhuǎn)換后得出新的數(shù)組。
參數(shù)
value: any
返回值
Object
API 說明
語法如下:
db.command.aggregate.map({
input: ,
as: ,
in:
})
| 字段 | 說明 |
|---|---|
| input | 一個(gè)可以解析為數(shù)組的表達(dá)式 |
| as | 可選,用于表示數(shù)組各個(gè)元素的變量,默認(rèn)為 this |
| in | 一個(gè)可以應(yīng)用在給定數(shù)組的各個(gè)元素上的表達(dá)式,各個(gè)元素的名字由 as 參數(shù)決定(參數(shù)名需加 $$ 前綴,如 $$this) |
示例代碼
假設(shè)集合 stats 有如下記錄:
{
"_id": 1,
"sales": [ 1.32, 6.93, 2.48, 2.82, 5.74 ]
}
{
"_id": 2,
"sales": [ 2.97, 7.13, 1.58, 6.37, 3.69 ]
}
將各個(gè)數(shù)字截?cái)酁檎危缓笄蠛?/p>
const $ = db.command.aggregate
db.collection('stats').aggregate()
.project({
truncated: $.map({
input: '$sales',
as: 'num',
in: $.trunc('$$num'),
})
})
.project({
total: $.sum('$truncated')
})
.end()
返回結(jié)果如下:
{ "_id": 1, "index": 16 }
{ "_id": 2, "index": 19 }AggregateCommand.objectToArray(value: Expression
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。將一個(gè)對象轉(zhuǎn)換為數(shù)組。方法把對象的每個(gè)鍵值對都變成輸出數(shù)組的一個(gè)元素,元素形如 { k: <key>, v: <value> }。
參數(shù)
value: Expression
返回值
Object
API 說明
語法如下:
db.command.aggregate.objectToArray(示例代碼
假設(shè)集合 items 有如下記錄:
{ "_id": 1, "attributes": { "color": "red", "price": 150 } }
{ "_id": 2, "attributes": { "color": "blue", "price": 50 } }
{ "_id": 3, "attributes": { "color": "yellow", "price": 10 } }
const $ = db.command.aggregate
db.collection('items').aggregate()
.project({
array: $.objectToArray('$attributes')
})
.end()
返回結(jié)果如下:
{ "_id": 1, "array": [{ "k": "color", "v": "red" }, { "k": "price", "v": 150 }] }
{ "_id": 2, "array": [{ "k": "color", "v": "blue" }, { "k": "price", "v": 50 }] }
{ "_id": 3, "array": [{ "k": "color", "v": "yellow" }, { "k": "price", "v": 10 }] }AggregateCommand.range(value: Expression[]): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。返回一組生成的序列數(shù)字。給定開始值、結(jié)束值、非零的步長,range 會(huì)返回從開始值開始逐步增長、步長為給定步長、但不包括結(jié)束值的序列。
參數(shù)
value: Expression[]
[
返回值
Object
API 說明
語法如下:
db.command.aggregate.range([, , ])
| 字段 | 說明 |
|---|---|
| start | 開始值,一個(gè)可以解析為整形的表達(dá)式 |
| end | 結(jié)束值,一個(gè)可以解析為整形的表達(dá)式 |
| non-zero step | 可選,步長,一個(gè)可以解析為非零整形的表達(dá)式,默認(rèn)為 1 |
示例代碼
假設(shè)集合 stats 有如下記錄:
{ "_id": 1, "max": 52 }
{ "_id": 2, "max": 38 }
const $ = db.command.aggregate
db.collection('stats').aggregate()
.project({
points: $.range([0, '$max', 10])
})
.end()
返回結(jié)果如下:
{ "_id": 1, "points": [0, 10, 20, 30, 40, 50] }
{ "_id": 2, "points": [0, 10, 20] }AggregateCommand.reduce(value: any): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。類似 JavaScript 的 reduce 方法,應(yīng)用一個(gè)表達(dá)式于數(shù)組各個(gè)元素然后歸一成一個(gè)元素。
參數(shù)
value: any
返回值
Object
API 說明
語法如下:
db.command.aggregate.reduce({
input:
initialValue: ,
in:
})
| 字段 | 說明 |
|---|---|
| input | 輸入數(shù)組,可以是任意解析為數(shù)組的表達(dá)式 |
| initialValue | 初始值 |
| in | 用來作用于每個(gè)元素的表達(dá)式,在 in 中有兩個(gè)可用變量,value 是表示累計(jì)值的變量,this 是表示當(dāng)前數(shù)組元素的變量 |
示例代碼
簡易字符串拼接
假設(shè)集合 player 有如下記錄:
{ "_id": 1, "fullname": [ "Stephen", "Curry" ] }
{ "_id": 2, "fullname": [ "Klay", "Thompsom" ] }
獲取各個(gè)球員的全名,并加 Player: 前綴:
const $ = db.command.aggregate
db.collection('player').aggregate()
.project({
info: $.reduce({
input: '$fullname',
initialValue: 'Player:',
in: $.concat(['$$value', ' ', '$$this']),
})
})
.end()
返回結(jié)果如下:
{ "_id": 1, "info": "Player: Stephen Curry" }
{ "_id": 2, "info": "Player: Klay Thompson" }
獲取各個(gè)球員的全名,不加前綴:
const $ = db.command.aggregate
db.collection('player').aggregate()
.project({
name: $.reduce({
input: '$fullname',
initialValue: '',
in: $.concat([
'$$value',
$.cond({
if: $.eq(['$$value', '']),
then: '',
else: ' ',
}),
'$$this',
]),
})
})
.end()
返回結(jié)果如下:
{ "_id": 1, "name": "Stephen Curry" }
{ "_id": 2, "name": "Klay Thompson" }AggregateCommand.reverseArray(value: Expression): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。返回給定數(shù)組的倒序形式。
參數(shù)
value: Expression
返回值
Object
API 說明
語法如下:
db.command.aggregate.reverseArray()
參數(shù)可以是任意解析為數(shù)組表達(dá)式。
示例代碼
假設(shè)集合 stats 有如下記錄:
{
"_id": 1,
"sales": [ 1, 2, 3, 4, 5 ]
}
取 sales 倒序:
const $ = db.command.aggregate
db.collection('stats').aggregate()
.project({
reversed: $.reverseArray('$sales'),
})
.end()
返回結(jié)果如下:
{ "_id": 1, "reversed": [5, 4, 3, 2, 1] }AggregateCommand.size(value: Expression): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。返回?cái)?shù)組長度。
參數(shù)
value: Expression
返回值
Object
API 說明
語法如下:
db.command.aggregate.size()
示例代碼
假設(shè)集合 shops 有如下記錄:
{ "_id": 1, "staff": [ "John", "Middleton", "George" ] }
{ "_id": 2, "staff": [ "Steph", "Jack" ] }
計(jì)算各個(gè)商店的雇員數(shù)量:
const $ = db.command.aggregate
db.collection('staff').aggregate()
.project({
totalStaff: $.size('$staff')
})
.end()
返回結(jié)果如下:
{ "_id": 1, "totalStaff": 3 }
{ "_id": 2, "totalStaff": 2 }AggregateCommand.slice(value: Expression[]): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。類似 JavaScritp 的 slice 方法。返回給定數(shù)組的指定子集。
參數(shù)
value: Expression[]
[
返回值
Object
API 說明
語法有兩種:
返回從開頭或結(jié)尾開始的 n 個(gè)元素:
db.command.aggregate.slice([, ])
返回從指定位置算作數(shù)組開頭、再向后或向前的 n 個(gè)元素:
db.command.aggregate.slice([, , ])
示例代碼
假設(shè)集合 people 有如下記錄:
{ "_id": 1, "hobbies": [ "basketball", "football", "tennis", "badminton" ] }
{ "_id": 2, "hobbies": [ "golf", "handball" ] }
{ "_id": 3, "hobbies": [ "table tennis", "swimming", "rowing" ] }
統(tǒng)一返回前兩個(gè)愛好:
const $ = db.command.aggregate
db.collection('fruits').aggregate()
.project({
hobbies: $.slice(['$hobbies', 2]),
})
.end()
返回結(jié)果如下:
{ "_id": 1, "hobbies": [ "basketball", "football" ] }
{ "_id": 2, "hobbies": [ "golf", "handball" ] }
{ "_id": 3, "hobbies": [ "table tennis", "swimming" ] }AggregateCommand.zip(value: any): Object
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
聚合操作符。把二維數(shù)組的第二維數(shù)組中的相同序號(hào)的元素分別拼裝成一個(gè)新的數(shù)組進(jìn)而組裝成一個(gè)新的二維數(shù)組。如可將 [ [ 1, 2, 3 ], [ "a", "b", "c" ] ] 轉(zhuǎn)換成 [ [ 1, "a" ], [ 2, "b" ], [ 3, "c" ] ]。
參數(shù)
value: any
返回值
Object
API 說明
語法如下:
db.command.aggregate.zip({
inputs: [, , ...],
useLongestLength: ,
defaults:
})
inputs 是一個(gè)二維數(shù)組(inputs 不可以是字段引用),其中每個(gè)元素的表達(dá)式(這個(gè)可以是字段引用)都可以解析為數(shù)組。如果其中任意一個(gè)表達(dá)式返回 null,
useLongestLength 決定輸出數(shù)組的長度是否采用輸入數(shù)組中的最長數(shù)組的長度。默認(rèn)為 false,即輸入數(shù)組中的最短的數(shù)組的長度即是輸出數(shù)組的各個(gè)元素的長度。
defaults 是一個(gè)數(shù)組,用于指定在輸入數(shù)組長度不一的情況下時(shí)采用的數(shù)組各元素默認(rèn)值。指定這個(gè)字段則必須指定 useLongestLength,否則返回錯(cuò)誤。如果 useLongestLength 是 true 但是 defaults 是空或沒有指定,則 zip 用 null 做數(shù)組元素的缺省默認(rèn)值。指定各元素默認(rèn)值時(shí) defaults 數(shù)組的長度必須是輸入數(shù)組最大的長度。
示例代碼
假設(shè)集合 stats 有如下記錄:
{ "_id": 1, "zip1": [1, 2], "zip2": [3, 4], "zip3": [5, 6] ] }
{ "_id": 2, "zip1": [1, 2], "zip2": [3], "zip3": [4, 5, 6] ] }
{ "_id": 3, "zip1": [1, 2], "zip2": [3] ] }
只傳 inputs
const $ = db.command.aggregate
db.collection('items').aggregate()
.project({
zip: $.zip({
inputs: [
'$zip1', // 字段引用
'$zip2',
'$zip3',
],
})
})
.end()
返回結(jié)果如下:
{ "_id": 1, "zip": [ [1, 3, 5], [2, 4, 6] ] }
{ "_id": 2, "zip": [ [1, 3, 4] ] }
{ "_id": 3, "zip": null }
設(shè)置 useLongestLength
如果設(shè) useLongestLength 為 true:
const $ = db.command.aggregate
db.collection('items').aggregate()
.project({
zip: $.zip({
inputs: [
'$zip1', // 字段引用
'$zip2',
'$zip3',
],
useLongestLength: true,
})
})
.end()
返回結(jié)果如下:
{ "_id": 1, "zip": [ [1, 3, 5], [2, 4, 6] ] }
{ "_id": 2, "zip": [ [1, 3, 4], [2, null, 5], [null, null, 6] ] }
{ "_id": 3, "zip": null }
設(shè)置 defaults
const $ = db.command.aggregate
db.collection('items').aggregate()
.project({
zip: $.zip({
inputs: [
'$zip1', // 字段引用
'$zip2',
'$zip3',
],
useLongestLength: true,
defaults: [-300, -200, -100],
})
})
.end()
返回結(jié)果如下:
{ "_id": 1, "zip": [ [1, 3, 5], [2, 4, 6] ] }
{ "_id": 2, "zip": [ [1, 3, 4], [2, -200, 5], [-300, -200, 6] ] }
{ "_id": 3, "zip": null } 新聞標(biāo)題:創(chuàng)新互聯(lián)小程序教程:SDK數(shù)據(jù)庫 Command·聚合操作符·數(shù)組操作符
本文URL:http://www.dlmjj.cn/article/cdjdijd.html


咨詢
建站咨詢

