Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aggregate function enhancements to include multiple fields without _id field

Tags:

mongodb

I am trying to convert my data to array list using the aggregate function. Below is my code

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

which gives me the following output

[ {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {} ]

If I remove the $project I get the below output with an added _id along with the data

[ { _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' } } ]

Question: How can I change the aggregate function code to get the following output

{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},
like image 202
Learningfromyou Avatar asked Dec 16 '25 20:12

Learningfromyou


2 Answers

When you use $group with multiple fields, all those fields go inside _id, so your documents after $group will look like the results you added in your question, to solve this, you can add fields you want to top level object.

add another pipeline after your $group to add these fields:

{
    $addFields: {
        total_quantity: "$_id.total_quantity",
        parent_asin: "$_id.parent_asin",
        product_asin: "$_id.product_asin"
    }
}

and then do the $project like before.

like image 170
Mo Ganji Avatar answered Dec 19 '25 13:12

Mo Ganji


access key using _id in project like this :

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
    }
  }
]);
like image 38
Vikash_Singh Avatar answered Dec 19 '25 13:12

Vikash_Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!