收藏
回答

云开发 数据库,如何合并数组中的2个重复结果?

"goods": [
          {
            "_id": "e2297d935ec3342c00d9125250f266421f",
            "num": 1
          },
          {
            "_id": "37e26adb5ec39b120102ae453b7ad2921",
            "num": 1
          },
          {
            "_id": "37e26adb5ec39b120102ae453b7ad2921",
            "gName": "可口可乐 300ml 12瓶装",
            "price": 0.01,
            "originalPrice": "80",
            "description": "炎炎下入,快乐肥宅水等你来喝",
            "classifyId": "aa9f906d5ec1f1b900fc5bdb2d680388",
            "photos": [
              "cloud://minorder-3f3365.6d69-minorder-3f3365-1257961996/photo/1589872751534953931.png",
              "cloud://minorder-3f3365.6d69-minorder-3f3365-1257961996/photo/1589877511535131244.png",
              "cloud://minorder-3f3365.6d69-minorder-3f3365-1257961996/photo/1589871751535152226.png"
            ],
            "_openid": "oKvsO0fqd22twirFCM12Tk1cVHnT0w",
            "today": true
          },
          {
            "_id": "e2297d935ec3342c00d915250f266421f",
            "classifyId": "5e847ab25ec384be0142adf92b765f7d",
            "photos": [
              "cloud://minorder-3f3365.6d69-minorder-3f3365-1257961996/photo/1589852117867166934.png",
              "cloud://minorder-3f3365.6d69-minorder-3f3365-1257961996/photo/1589285117867360109.png",
              "cloud://minorder-3f3365.6d69-minorder-3f3365-1257961996/photo/1589285117867441021.png"
            ],
            "_openid": "oKvsO0fqd2t2wirFCM2Tk1cVHnT0w",
            "gName": "山西苹果 丑的更好吃",
            "price": 0.01,
            "originalPrice": "16",
            "description": "12月份最新鲜的苹果,只有30斤,卖了就没有了。",
            "today": true
          }
        ]


云开发,是否有办法,把重复的id对象合并,上面是4个结果,需要合并成2个结果

回答关注问题邀请回答
收藏

2 个回答

  • 娃娃挖挖蛙
    娃娃挖挖蛙
    2020-07-20
    { "group": "a", "name": "stu1", "score": 84 }
    { "group": "a", "name": "stu2", "score": 96 }
    { "group": "b", "name": "stu3", "score": 80 }
    { "group": "b", "name": "stu4", "score": 100 }
    
    const $ = db.command.aggregate
    db
      .collection('students')
      .aggregate()
      .group({
        _id: '$group',
        students: $.push({
          name: '$name',
          score: '$score'
        })
      })
      .end()
    

    输出结果如下:

    { "_id": "b", "students": [{ "name": "stu3", "score": 80 }, { "name": "stu4", "score": 100 }] }
    { "_id": "a", "students": [{ "name": "stu1", "score": 84 }, { "name": "stu2", "score": 96 }] }
    
    参考文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-sdk-api/database/command/aggregate/AggregateCommand.push.html
    
    2020-07-20
    有用
    回复 1
    • 王政娇
      王政娇
      2020-07-28
      感谢,需求是要合并重复项保留不同的属性,找到了一个方案,如下。
      2020-07-28
      回复
  • xplee
    xplee
    2020-05-27

    lodash库了解下

    2020-05-27
    有用
    回复 2
    • 王政娇
      王政娇
      2020-05-27
      这是云开发的数据库操作,不是js对象操作呢。
      2020-05-27
      回复
    • xplee
      xplee
      2020-05-27回复王政娇
      没辙
      2020-05-27
      回复
登录 后发表内容
问题标签