首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >聚合函数增强以包含不带_id字段的多个字段

聚合函数增强以包含不带_id字段的多个字段
EN

Stack Overflow用户
提问于 2019-05-13 01:09:20
回答 2查看 244关注 0票数 3

我试图使用聚合函数将数据转换为数组列表。下面是我的代码

代码语言:javascript
复制
Quantity.aggregate([
    {$group: {
                    _id: {
                        product_asin: "$productAsin",
                        parent_asin: "$parentProductAsin",
                        total_quantity: "$totalQuantity" ,                  
                    }
    }},
     { "$project":{product_asin:true,parent_asin:true,total_quantity:true,_id:false}}
 ])

它提供了以下输出

代码语言:javascript
复制
[ {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} ]

如果删除$project,将得到以下输出,并添加了一个_id和数据

代码语言:javascript
复制
[ { _id:
     { product_asin: 'asdasd',
       parent_asin: 'Dasda',
       total_quantity: '0' } },
  { _id:
     { product_asin: 'dfasf',
       parent_asin: 'fasd',
       total_quantity: '0' } },
  { _id:
     { product_asin: 'fasd',
       parent_asin: 'fasd',
       total_quantity: '0' } },
  { _id:
     { product_asin: 'fasd',
       parent_asin: 'asd',
       total_quantity: '0' } },
  { _id:
     { product_asin: 'fsda',
       parent_asin: 'asdf',
       total_quantity: '0' } },
  { _id:
     { product_asin: 'asd',
       parent_asin: 'asd',
       total_quantity: '0' } },
  { _id:
     { product_asin: 'fsda',
       parent_asin: 'asdf',
       total_quantity: '0' } },
  { _id:
     { product_asin: 'fasd',
       parent_asin: 'asdf',
       total_quantity: '0' } },
  { _id:
     { product_asin: 'fsda',
       parent_asin: 'asdf',
       total_quantity: '0' } },
  { _id:
     { product_asin: 'fsda',
       parent_asin: 'asdf',
       total_quantity: '0' } },
  { _id:
     { product_asin: 'fsadfsa',
       parent_asin: 'asdf',
       total_quantity: '0' } },
  { _id:
     { product_asin: 'asdf',
       parent_asin: 'sadf',
       total_quantity: '0' } },
  { _id:
     { product_asin: 'fsda',
       parent_asin: 'asdfasdf',
       total_quantity: '0' } },
  { _id:
     { product_asin: 'fasda',
       parent_asin: 'fasd',
       total_quantity: '0' } } ]

问题:如何更改聚合函数代码以获得以下输出

代码语言:javascript
复制
{product_asin: 'IBM', parent_asin: 13, total_quantity: 12},
  {product_asin: 'IB2342M', parent_asin: 13, total_quantity: 12},
  {product_asin: 'I44234BM', parent_asin: 14,  total_quantity: 12},
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-13 07:44:04

当您在多个字段中使用$group时,所有这些字段都在_id中,因此您在$group之后的文档将类似于您在问题中添加的结果,为了解决这个问题,可以将希望添加到顶层对象的字段添加到中。

$group之后添加另一个管道以添加以下字段:

代码语言:javascript
复制
{
    $addFields: {
        total_quantity: "$_id.total_quantity",
        parent_asin: "$_id.parent_asin",
        product_asin: "$_id.product_asin"
    }
}

然后像以前一样做$project

票数 1
EN

Stack Overflow用户

发布于 2019-05-13 10:53:54

在项目中使用_id访问密钥如下:

代码语言:javascript
复制
Quantity.aggregate([{
    $group: {
      _id: {
        product_asin: "$productAsin",
        parent_asin: "$parentProductAsin",
        total_quantity: "$totalQuantity"
      }
    }
  },
  {
    $project: {
      product_asin: "$_id.product_asin",
      parent_asin: "$_id.parent_asin",
      total_quantity: "$_id.total_quantity",
      _id: 0
    }
  }
]);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56104751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档