MongoDB Aggregation with sum of array object values from multiple document [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1
















This question already has an answer here:




  • Get sum of Nested Array in Aggregate

    1 answer



  • MongoDB group by array inner-elements

    1 answer




I have a collection with the following data:



{
"_id": "1",
"arrlstdetails": [{
"_id": "1",
"quantity": 35.5,
"units": "m3"
}]
},{
"_id": "2",
"arrlstdetails": [{
"_id": "1",
"quantity": 35.5,
"units": "m3"
},
{
"_id": "2",
"quantity": 12.25,
"units": "m3"
},
{
"_id": "3",
"quantity": 28.20,
"units": "t"
}
]
}


I would like to perform the MongoDB Aggregation for a sum of quantity filtering by unit(m3 or t).



And this is the result that I would like to get after the querying:



Example: 1.



{
"Unit":"m3",
"Total quantity sum":"83.25"
}


Example: 2.



{
"Unit":"t",
"Total quantity sum":"28.20"
}


How can I query to get the sum of quantity?










share|improve this question













marked as duplicate by Neil Lunn aggregation-framework
Users with the  aggregation-framework badge can single-handedly close aggregation-framework questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 20:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You should refer at least MongoDB document and try to put your code whatever you have tried. Though please check the answer.

    – Hardik Shah
    Nov 23 '18 at 13:29


















1
















This question already has an answer here:




  • Get sum of Nested Array in Aggregate

    1 answer



  • MongoDB group by array inner-elements

    1 answer




I have a collection with the following data:



{
"_id": "1",
"arrlstdetails": [{
"_id": "1",
"quantity": 35.5,
"units": "m3"
}]
},{
"_id": "2",
"arrlstdetails": [{
"_id": "1",
"quantity": 35.5,
"units": "m3"
},
{
"_id": "2",
"quantity": 12.25,
"units": "m3"
},
{
"_id": "3",
"quantity": 28.20,
"units": "t"
}
]
}


I would like to perform the MongoDB Aggregation for a sum of quantity filtering by unit(m3 or t).



And this is the result that I would like to get after the querying:



Example: 1.



{
"Unit":"m3",
"Total quantity sum":"83.25"
}


Example: 2.



{
"Unit":"t",
"Total quantity sum":"28.20"
}


How can I query to get the sum of quantity?










share|improve this question













marked as duplicate by Neil Lunn aggregation-framework
Users with the  aggregation-framework badge can single-handedly close aggregation-framework questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 20:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • You should refer at least MongoDB document and try to put your code whatever you have tried. Though please check the answer.

    – Hardik Shah
    Nov 23 '18 at 13:29














1












1








1


2







This question already has an answer here:




  • Get sum of Nested Array in Aggregate

    1 answer



  • MongoDB group by array inner-elements

    1 answer




I have a collection with the following data:



{
"_id": "1",
"arrlstdetails": [{
"_id": "1",
"quantity": 35.5,
"units": "m3"
}]
},{
"_id": "2",
"arrlstdetails": [{
"_id": "1",
"quantity": 35.5,
"units": "m3"
},
{
"_id": "2",
"quantity": 12.25,
"units": "m3"
},
{
"_id": "3",
"quantity": 28.20,
"units": "t"
}
]
}


I would like to perform the MongoDB Aggregation for a sum of quantity filtering by unit(m3 or t).



And this is the result that I would like to get after the querying:



Example: 1.



{
"Unit":"m3",
"Total quantity sum":"83.25"
}


Example: 2.



{
"Unit":"t",
"Total quantity sum":"28.20"
}


How can I query to get the sum of quantity?










share|improve this question















This question already has an answer here:




  • Get sum of Nested Array in Aggregate

    1 answer



  • MongoDB group by array inner-elements

    1 answer




I have a collection with the following data:



{
"_id": "1",
"arrlstdetails": [{
"_id": "1",
"quantity": 35.5,
"units": "m3"
}]
},{
"_id": "2",
"arrlstdetails": [{
"_id": "1",
"quantity": 35.5,
"units": "m3"
},
{
"_id": "2",
"quantity": 12.25,
"units": "m3"
},
{
"_id": "3",
"quantity": 28.20,
"units": "t"
}
]
}


I would like to perform the MongoDB Aggregation for a sum of quantity filtering by unit(m3 or t).



And this is the result that I would like to get after the querying:



Example: 1.



{
"Unit":"m3",
"Total quantity sum":"83.25"
}


Example: 2.



{
"Unit":"t",
"Total quantity sum":"28.20"
}


How can I query to get the sum of quantity?





This question already has an answer here:




  • Get sum of Nested Array in Aggregate

    1 answer



  • MongoDB group by array inner-elements

    1 answer








mongodb mongodb-query aggregation-framework






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 12:34









Harshal YelpaleHarshal Yelpale

12613




12613




marked as duplicate by Neil Lunn aggregation-framework
Users with the  aggregation-framework badge can single-handedly close aggregation-framework questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 20:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Neil Lunn aggregation-framework
Users with the  aggregation-framework badge can single-handedly close aggregation-framework questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 20:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • You should refer at least MongoDB document and try to put your code whatever you have tried. Though please check the answer.

    – Hardik Shah
    Nov 23 '18 at 13:29



















  • You should refer at least MongoDB document and try to put your code whatever you have tried. Though please check the answer.

    – Hardik Shah
    Nov 23 '18 at 13:29

















You should refer at least MongoDB document and try to put your code whatever you have tried. Though please check the answer.

– Hardik Shah
Nov 23 '18 at 13:29





You should refer at least MongoDB document and try to put your code whatever you have tried. Though please check the answer.

– Hardik Shah
Nov 23 '18 at 13:29












1 Answer
1






active

oldest

votes


















4














Use $unwind and $group



db.getCollection('test').aggregate([
{ $unwind: "$arrlstdetails" },
{ $group : {
_id : "$arrlstdetails.units",
"Total quantity sum" : {$sum : "$arrlstdetails.quantity"}
}}
])





share|improve this answer
























  • Perfect!!. This is what I was looking for :)

    – Harshal Yelpale
    Nov 26 '18 at 9:48


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Use $unwind and $group



db.getCollection('test').aggregate([
{ $unwind: "$arrlstdetails" },
{ $group : {
_id : "$arrlstdetails.units",
"Total quantity sum" : {$sum : "$arrlstdetails.quantity"}
}}
])





share|improve this answer
























  • Perfect!!. This is what I was looking for :)

    – Harshal Yelpale
    Nov 26 '18 at 9:48
















4














Use $unwind and $group



db.getCollection('test').aggregate([
{ $unwind: "$arrlstdetails" },
{ $group : {
_id : "$arrlstdetails.units",
"Total quantity sum" : {$sum : "$arrlstdetails.quantity"}
}}
])





share|improve this answer
























  • Perfect!!. This is what I was looking for :)

    – Harshal Yelpale
    Nov 26 '18 at 9:48














4












4








4







Use $unwind and $group



db.getCollection('test').aggregate([
{ $unwind: "$arrlstdetails" },
{ $group : {
_id : "$arrlstdetails.units",
"Total quantity sum" : {$sum : "$arrlstdetails.quantity"}
}}
])





share|improve this answer













Use $unwind and $group



db.getCollection('test').aggregate([
{ $unwind: "$arrlstdetails" },
{ $group : {
_id : "$arrlstdetails.units",
"Total quantity sum" : {$sum : "$arrlstdetails.quantity"}
}}
])






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 13:29









Hardik ShahHardik Shah

1,7422824




1,7422824













  • Perfect!!. This is what I was looking for :)

    – Harshal Yelpale
    Nov 26 '18 at 9:48



















  • Perfect!!. This is what I was looking for :)

    – Harshal Yelpale
    Nov 26 '18 at 9:48

















Perfect!!. This is what I was looking for :)

– Harshal Yelpale
Nov 26 '18 at 9:48





Perfect!!. This is what I was looking for :)

– Harshal Yelpale
Nov 26 '18 at 9:48





Popular posts from this blog

Paul Cézanne

UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

Angular material date-picker (MatDatepicker) auto completes the date on focus out