如何将数组中的相同二级分类再合并?
发布于 5 年前 作者 baiming 6688 次浏览 来自 官方Issues

原始数据:[{

{

"_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"

}

]


2 回复
勉强实现
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()
}

// 云函数入口文件

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

     })

}

回到顶部