日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)小程序教程:SDK數(shù)據(jù)庫(kù)Command·聚合操作符·對(duì)象操作符

AggregateCommand.mergeObjects(value: Expression): Object

支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web

聚合操作符。將多個(gè)文檔合并為單個(gè)文檔。

參數(shù)

value: Expression

Document 表達(dá)式

返回值

Object

API 說(shuō)明

使用形式如下: 在 group() 中使用時(shí):

mergeObjects()

在其它表達(dá)式中使用時(shí):

mergeObjects([, , ...])

示例代碼

搭配 group() 使用

假設(shè)集合 sales 存在以下文檔:

{ "_id": 1, "year": 2018, "name": "A", "volume": { "2018Q1": 500, "2018Q2": 500 } }
{ "_id": 2, "year": 2017, "name": "A", "volume": { "2017Q1": 400, "2017Q2": 300, "2017Q3": 0, "2017Q4": 0 } }
{ "_id": 3, "year": 2018, "name": "B", "volume": { "2018Q1": 100 } }
{ "_id": 4, "year": 2017, "name": "B", "volume": { "2017Q3": 100, "2017Q4": 250 } }

下面的代碼使用 mergeObjects(),將用相同 name 的文檔合并:

const $ = db.command.aggregate
db.collection('sales').aggregate()
  .group({
    _id: '$name',
    mergedVolume: $.mergeObjects('$volume')
  })
  .end()

輸出如下:

{ "_id": "A", "mergedVolume": { "2017Q1": 400, "2017Q2": 300, "2017Q3": 0, "2017Q4": 0, "2018Q1": 500, "2018Q2": 500 } }
{ "_id": "B", "mergedVolume": { "2017Q3": 100, "2017Q4": 250, "2018Q1": 100 } }

一般用法

假設(shè)集合 test 存在以下文檔:

{ "_id": 1, "foo": { "a": 1 }, "bar": { "b": 2 } }
{ "_id": 2, "foo": { "c": 1 }, "bar": { "d": 2 } }
{ "_id": 3, "foo": { "e": 1 }, "bar": { "f": 2 } }

下面的代碼使用 mergeObjects(),將文檔中的 foo 和 bar 字段合并為 foobar:

const $ = db.command.aggregate
db.collection('sales').aggregate()
  .project({
    foobar: $.mergeObjects(['$foo', '$bar'])
  })
  .end()

輸出結(jié)果如下:

{ "_id": 1, "foobar": { "a": 1, "b": 2 } }
{ "_id": 2, "foobar": { "c": 1, "d": 2 } }
{ "_id": 3, "foobar": { "e": 1, "f": 2 } }

AggregateCommand.objectToArray(value: Expression): Object

支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web

聚合操作符。將一個(gè)對(duì)象轉(zhuǎn)換為數(shù)組。方法把對(duì)象的每個(gè)鍵值對(duì)都變成輸出數(shù)組的一個(gè)元素,元素形如 { k: <key>, v: <value> }。

參數(shù)

value: Expression

返回值

Object

API 說(shuō)明

語(yǔ)法如下:

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 }] }


文章名稱:創(chuàng)新互聯(lián)小程序教程:SDK數(shù)據(jù)庫(kù)Command·聚合操作符·對(duì)象操作符
URL標(biāo)題:http://www.5511xx.com/article/dhccsdi.html