189 8069 5689

创新互联小程序教程:SDK数据库Command·聚合操作符·对象操作符

AggregateCommand.mergeObjects(value: Expression): Object

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

聚合操作符。将多个文档合并为单个文档。

参数

value: Expression

Document 表达式

返回值

Object

API 说明

使用形式如下: 在 group() 中使用时:

mergeObjects()

在其它表达式中使用时:

mergeObjects([, , ...])

示例代码

搭配 group() 使用

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

一般用法

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

输出结果如下:

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

聚合操作符。将一个对象转换为数组。方法把对象的每个键值对都变成输出数组的一个元素,元素形如 { k: <key>, v: <value> }。

参数

value: Expression

返回值

Object

API 说明

语法如下:

db.command.aggregate.objectToArray()

示例代码

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

返回结果如下:

{ "_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 }] }


新闻标题:创新互联小程序教程:SDK数据库Command·聚合操作符·对象操作符
文章源于:http://www.ptruijie.cn/article/dhccsdi.html

联系我们

您好HELLO!
感谢您来到新都网站建设公司,若您有合作意向,请您为我们留言或使用以下方式联系我们, 我们将尽快给你回复,并为您提供真诚的设计服务,谢谢。
  • 电话:028- 86922220 18980695689
  • 商务合作邮箱:631063699@qq.com
  • 合作QQ: 532337155
  • 成都网站设计地址:成都市青羊区锣锅巷31号五金站写字楼6楼

广皓图文建站工作室

新都广皓图文网站建设公司拥有多年以上互联网从业经验的团队,始终保持务实的风格,以"帮助客户成功"为已任,专注于提供对客户有价值的服务。 我们已为众企业及上市公司提供专业的网站建设服务。我们不只是一家网站建设的网络公司;我们对营销、技术、管理都有自己独特见解,广皓图文建站采取“创意+综合+营销”一体化的方式为您提供更专业的服务!

广皓观点

相对传统的新都网站建设公司而言,广皓图文是互联网中的网站品牌策划,我们精于企业品牌与互联网相结合的整体战略服务。
我们始终认为,网站必须注入企业基因,真正使网站成为企业vi的一部分,让整个网站品牌策划体系变的深入而持久。