mongodb分组且提取组内所有数据到一个数组里面方式

 

db.tempdata.insertMany([
    {
    name: "AAA",
    age: 14,
    country: "us"
  },
  {
    name: "BBB",
    age: 13,
    country: "us"
  },
  {
    name: "CCC",
    age: 12,
    country: null
  },
  {
    name: "DDD",
    age: 12,
    country: null
  }
    ])
    
 

db.tempdata.aggregate([{
    
    $group: { _id: "$country",
        all:{
            $push: "$$ROOT"
        }
    }
}])

image

posted @ 2023-03-28 18:14  沙漠皇帝  阅读(82)  评论(0编辑  收藏  举报