如何将 MongoDB 长格式 collection 导出为宽格式 csv

How to export MongoDB long format collection as wide format csv

我有mongodbcollection格式如下

brand1,trouser
brand1,jeans
brand2,belt
brand2,shoes
brand2,jeans

我想将此数据导出为以下格式的 csv 文件

brand1,trouser,jeans,belt
brand2,belt,shoes,jeans

谁能帮帮我。

我设法得到了问题的答案。

db.brand.aggregate([{ $group : { _id: "$brand_id", "items": { $push: '$item' } }}], {allowDiskUse : true})

结果如下:

{ "_id" : "brand1","items" : [ 'trouser','jeans'] }

{ "_id" : "brand2","items" : [ 'belt','shoes','jeans'] }