SDK数据库 Command聚合操作符数组操作符


本文整理自网络,侵删。

AggregateCommand.arrayElemAt(value: Expression[]): Object

支持端:小程序 2.7.4, 云函数 0.8.1, Web

聚合操作符。返回在指定数组下标的元素。

参数

value: Expression[]

[<array>, <index>]

返回值

Object

API 说明

语法如下:

db.command.aggregate.arrayElemAt([<array>, <index>])

<array> 可以是任意解析为数字的表达式。

<index> 可以是任意解析为整形的表达式。如果是正数,arrayElemAt 返回在 index 位置的元素,如果是负数,arrayElemAt 返回从数组尾部算起的 index 位置的元素。

示例代码

假设集合 exams 有如下记录:

{ "_id": 1, "scores": [80, 60, 65, 90] }
{ "_id": 2, "scores": [78] }
{ "_id": 3, "scores": [95, 88, 92] }

求各个第一次考试的分数和和最后一次的分数:

const $ = db.command.aggregate
db.collection('exams').aggregate()
  .project({
    first: $.arrayElemAt(['$scores', 0]),
    last: $.arrayElemAt(['$scores', -1]),
  })
  .end()

返回结果如下:

{ "_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, 云函数 0.8.1, Web

聚合操作符。将一个数组转换为对象。

参数

value: any

返回值

Object

API 说明

语法可以取两种:

第一种:传入一个二维数组,第二维的数组长度必须为 2,其第一个值为字段名,第二个值为字段值

db.command.aggregate.arrayToObject([
  [<key1>, <value1>],
  [<key2>, <value2>],
  ...
])

第二种:传入一个对象数组,各个对象必须包含字段 k 和 v,分别指定字段名和字段值

db.command.aggregate.arrayToObject([
  { "k": <key1>, "v": <value1> },
  { "k": <key2>, "v": <value2> },
  ...
])

传入 arrayToObject 的参数只要可以解析为上述两种表示法之一即可。

示例代码

假设集合 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 } ] }

求各个第一次考试的分数和和最后一次的分数:

const $ = db.command.aggregate
db.collection('shops').aggregate()
  .project({
    sales: $.arrayToObject('$sales'),
  })
  .end()

返回结果如下:

{ "_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, 云函数 0.8.1, Web

聚合操作符。将多个数组拼接成一个数组。

参数

value: Expression[]

[ <array1>, <array2>, ... ]

返回值

Object

API 说明

语法如下:

db.command.aggregate.arrayToObject([ <array1>, <array2>, ... ])

参数可以是任意解析为数组的表达式。

示例代码

假设集合 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()

返回结果如下:

{ "_id": 1, "list": [ "apple", "carrot" ] }
{ "_id": 2, "list": [ "orange", "lemon", "cabbage" ] }
{ "_id": 3, "list": [ "strawberry", "spinach" ] }

AggregateCommand.filter(value: any): Object

支持端:小程序 2.7.4, 云函数 0.8.1, Web

聚合操作符。根据给定条件返回满足条件的数组的子集。

参数

value: any

返回值

Object

API 说明

语法如下:

db.command.aggregate.filter({
  input: <array>,
  as: <string>,
  cond: <expression>
})
字段 说明
input 一个可以解析为数组的表达式
as 可选,用于表示数组各个元素的变量,默认为 this
cond 一个可以解析为布尔值的表达式,用于判断各个元素是否满足条件,各个元素的名字由 as 参数决定(参数名需加 $$ 前缀,如 $$this

参数可以是任意解析为数组的表达式。

示例代码

假设集合 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()

返回结果如下:

{ "_id": 1, "stock": [ { "name": "orange", "price": 20} ] }
{ "_id": 2, "stock": [ { "name": "lemon", "price": 15 } ] }

AggregateCommand.in(value: Expression[]): Object

支持端:小程序 2.7.4, 云函数 0.8.1, Web

聚合操作符。给定一个值和一个数组,如果值在数组中则返回 true,否则返回 false。

参数

value: Expression[]

[<value>, <array>]

返回值

Object

API 说明

语法如下:

db.command.aggregate.in([<value>, <array>])

<value> 可以是任意表达式。

阅读剩余部分

相关阅读 >>

微信小程序 wxs

微信小程序api-设备-扫码

微信小程序前端源码逻辑和工作流程解析

微信小程序 小程序使用mockupdateorder

微信小程序 服务市场invokeservice

微信小程序 欠费支付

微信小程序api 音频-创建audiocontext 对象。

微信小程序 sitemap配置

微信小程序 createqrcode

微信小程序云开发 api云函数

更多相关阅读请进入《微信小程序》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    正在狠努力加载,请稍候...