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;
}
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?
mongodb mongodb-query aggregation-framework
marked as duplicate by Neil Lunn
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.
add a comment |
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?
mongodb mongodb-query aggregation-framework
marked as duplicate by Neil Lunn
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
add a comment |
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?
mongodb mongodb-query aggregation-framework
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
mongodb mongodb-query aggregation-framework
asked Nov 23 '18 at 12:34
Harshal YelpaleHarshal Yelpale
12613
12613
marked as duplicate by Neil Lunn
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
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
Use $unwind and $group
db.getCollection('test').aggregate([
{ $unwind: "$arrlstdetails" },
{ $group : {
_id : "$arrlstdetails.units",
"Total quantity sum" : {$sum : "$arrlstdetails.quantity"}
}}
])
Perfect!!. This is what I was looking for :)
– Harshal Yelpale
Nov 26 '18 at 9:48
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use $unwind and $group
db.getCollection('test').aggregate([
{ $unwind: "$arrlstdetails" },
{ $group : {
_id : "$arrlstdetails.units",
"Total quantity sum" : {$sum : "$arrlstdetails.quantity"}
}}
])
Perfect!!. This is what I was looking for :)
– Harshal Yelpale
Nov 26 '18 at 9:48
add a comment |
Use $unwind and $group
db.getCollection('test').aggregate([
{ $unwind: "$arrlstdetails" },
{ $group : {
_id : "$arrlstdetails.units",
"Total quantity sum" : {$sum : "$arrlstdetails.quantity"}
}}
])
Perfect!!. This is what I was looking for :)
– Harshal Yelpale
Nov 26 '18 at 9:48
add a comment |
Use $unwind and $group
db.getCollection('test').aggregate([
{ $unwind: "$arrlstdetails" },
{ $group : {
_id : "$arrlstdetails.units",
"Total quantity sum" : {$sum : "$arrlstdetails.quantity"}
}}
])
Use $unwind and $group
db.getCollection('test').aggregate([
{ $unwind: "$arrlstdetails" },
{ $group : {
_id : "$arrlstdetails.units",
"Total quantity sum" : {$sum : "$arrlstdetails.quantity"}
}}
])
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
add a comment |
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
add a comment |
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