收藏
回答

如何将数组中的相同二级分类再合并?

原始数据:[{

{

"_id": "8d65afde5e4d2a79010af3ae173f3c31",

"imageC": "http://tmp/wx9696d4e5754a92b0.o6zAJs_sYdqkP5OplRpgfPIz-Vis.9cIQCAVc8Pe4a3f590515973ba69b44c5f86b56dff81.png",

"nameA": "A",

"nameB": "A3",

"nameC": "E3333"

},

{

"_id": "8d65afde5e4d2a79010af3b03f094576",

"imageC": "http://tmp/wx9696d4e5754a92b0.o6zAJs_sYdqkP5OplRpgfPIz-Vis.9cIQCAVc8Pe4a3f590515973ba69b44c5f86b56dff81.png",

"nameA": "A",

"nameB": "A3",

"nameC": "E3333"

},

{

"_id": "fb16f7905e4d2c00010c97000eab34a6",

"imageC": "http://tmp/wx9696d4e5754a92b0.o6zAJs_sYdqkP5OplRpgfPIz-Vis.9cIQCAVc8Pe4a3f590515973ba69b44c5f86b56dff81.png",

"nameA": "B",

"nameB": "A5",

"nameC": "E3333"

},

{

"_id": "4278fc3a5e4d2c080109a9095277efc3",

"imageC": "http://tmp/wx9696d4e5754a92b0.o6zAJs_sYdqkP5OplRpgfPIz-Vis.9cIQCAVc8Pe4a3f590515973ba69b44c5f86b56dff81.png",

"nameA": "B",

"nameB": "A5",

"nameC": "E222222"

},

{

"_id": "fb16f7905e4d2c0f010c9de51a769a17",

"imageC": "http://tmp/wx9696d4e5754a92b0.o6zAJs_sYdqkP5OplRpgfPIz-Vis.9cIQCAVc8Pe4a3f590515973ba69b44c5f86b56dff81.png",

"nameA": "B",

"nameB": "A5",

"nameC": "E11111"

},

{

"_id": "e30d61715e4d2c15010a550712a94455",

"imageC": "http://tmp/wx9696d4e5754a92b0.o6zAJs_sYdqkP5OplRpgfPIz-Vis.9cIQCAVc8Pe4a3f590515973ba69b44c5f86b56dff81.png",

"nameA": "B",

"nameB": "A4",

"nameC": "E11111"

},

{

"_id": "7d79e8035e4d2c1b010b25d421d9aabc",

"imageC": "http://tmp/wx9696d4e5754a92b0.o6zAJs_sYdqkP5OplRpgfPIz-Vis.9cIQCAVc8Pe4a3f590515973ba69b44c5f86b56dff81.png",

"nameA": "B",

"nameB": "A4",

"nameC": "E222222"

},

{

"_id": "7d79e8035e4d2c23010b29a05652d1ad",

"imageC": "http://tmp/wx9696d4e5754a92b0.o6zAJs_sYdqkP5OplRpgfPIz-Vis.9cIQCAVc8Pe4a3f590515973ba69b44c5f86b56dff81.png",

"nameA": "B",

"nameB": "A4",

"nameC": "E3333"

},

{

"_id": "8d65afde5e4d2c2b010bd06b467f35c3",

"imageC": "http://tmp/wx9696d4e5754a92b0.o6zAJs_sYdqkP5OplRpgfPIz-Vis.9cIQCAVc8Pe4a3f590515973ba69b44c5f86b56dff81.png",

"nameA": "B",

"nameB": "A3",

"nameC": "E3333"

},

{

"_id": "e30d61715e4d2c30010a62d60a9231b1",

"imageC": "http://tmp/wx9696d4e5754a92b0.o6zAJs_sYdqkP5OplRpgfPIz-Vis.9cIQCAVc8Pe4a3f590515973ba69b44c5f86b56dff81.png",

"nameA": "B",

"nameB": "A3",

"nameC": "E2222"

}

]





{

全成后希望变成相同的nameB的image子项并成一个数组,请指教!!!

[

{

"_id": "C",

"child": [

{

"nameB": "A3",

"image": [{

"nameC": "E1111",

"imageC": "http://tmp/wx9696d4e5754a92b0.o6zAJs_sYdqkP5OplRpgfPIz-Vis.9cIQCAVc8Pe4a3f590515973ba69b44c5f86b56dff81.png"

},

{

"nameC": "E22222",

"imageC": "http://tmp/wx9696d4e5754a92b0.o6zAJs_sYdqkP5OplRpgfPIz-Vis.9cIQCAVc8Pe4a3f590515973ba69b44c5f86b56dff81.png"

},

{

"nameC": "E33333",

"imageC": "http://tmp/wx9696d4e5754a92b0.o6zAJs_sYdqkP5OplRpgfPIz-Vis.9cIQCAVc8Pe4a3f590515973ba69b44c5f86b56dff81.png"

}

]


最后一次编辑于  2020-02-23
回答关注问题邀请回答
收藏

2 个回答

  • Mr.Zhao
    Mr.Zhao
    2020-02-23
    勉强实现
    const cloud = require('wx-server-sdk')
    cloud.init()
    const db = cloud.database();
    const $ = db.command.aggregate
    exports.main = async(event, context) => {
      return await db.collection('test').aggregate()
        .lookup({
          from: 'test',
          localField: 'nameA',
          foreignField: 'nameA',
          as: 'bookList',
        })
        .project({
          nameA: 1,
          nameB: 1,
          bookList: 1
        })
        .replaceRoot({
          newRoot: {
            nameA: "$nameA",
            nameB: "$nameB",
            image: "$bookList"
          }
        })
        .group({
          _id: '$nameA',
          child: $.addToSet({
            nameB: "$nameB",
            image: $.filter({
              input: '$image',
              as: 'item',
              cond: $.eq(['$$item.nameB', '$nameB'])
            })
          })
        })
        .end()
    }
    
    2020-02-23
    有用 1
    回复 32
    • Mr.Zhao
      Mr.Zhao
      2020-02-23
      2020-02-23
      回复
    • 朱_kambo
      朱_kambo
      2020-02-24回复Mr.Zhao
      在云数据库里调试是可以的,但不知为什么写在云函数时就取不出数据!
      2020-02-24
      回复
    • 朱_kambo
      朱_kambo
      2020-02-24回复Mr.Zhao
      帮我看下是什么问题,谢谢
      2020-02-24
      回复
    • Mr.Zhao
      Mr.Zhao
      2020-02-24回复朱_kambo
      重新部署下吧,因为我的没问题
      2020-02-24
      回复
    • 朱_kambo
      朱_kambo
      2020-02-24回复Mr.Zhao
      但我不知道问匙在那??
      2020-02-24
      回复
    查看更多(27)
  • 朱_kambo
    朱_kambo
    2020-02-24

    // 云函数入口文件

    const cloud = require('wx-server-sdk')

    cloud.init()

    const db=cloud.database()

    const _ = db.command

    const $ = db.command.aggregate

    // 云函数入口函数

    exports.main = async(event, context) => {

      const wxContext = cloud.getWXContext()

      return new Promise((resolve,reject)=>{

        db.collection('category').aggregate()

        .lookup({

          from: 'category',

          localField: 'nameA',

          foreignField: 'nameA',

          as: 'bookList',

        })

        .project({

          nameA: 1,

          nameB: 1,

          bookList: 1

        })

        .replaceRoot({

          newRoot: {

            nameA: '$nameA',

            nameB: '$nameB',

            images: '$bookList'

          }

        })

        .group({

          _id: '$nameA',

          child: $.addToSet({

            nameB: '$nameB',

            images: $.filter({

              input: '$images',

              as: 'item',

              cond: $.eq(['$$item.nameB', '$nameB'])

            })

          })

        })

        .end()

        .then((res)=>{resolve(res)})

        .catch((err)=>{console.log(err)})

         })

    }


    2020-02-24
    有用
    回复
登录 后发表内容
问题标签