I want to sort data based on date and the createdFor
property. I want the sum of the data in group of createdFor
and all specifc itemName
for each createdFor
.
date = [Tue Apr 05 2022 11:10:41 GMT-0400 (Eastern Daylight Time), Sat Apr 09 2022 11:10:41 GMT-0400 (Eastern Daylight Time)]
createdfor can be repeate and itemName is different for all createdFor
date = [Tue Apr 05 2022 11:10:41 GMT-0400 (Eastern Daylight Time), Sat Apr 09 2022 11:10:41 GMT-0400 (Eastern Daylight Time)]
const dataList = [
{
createdFor: "62429fd5f7db3912fea7a716",
itemName: "a",
totalQty: 100,
createAt: "2022-04-05T08:05:41.653+00:00",
},
{
createdFor: "62429fd5f7db3912fea7a716",
itemName: "b",
totalQty: 80,
createAt: "2022-04-08T08:05:41.653+00:00",
},
{
createdFor: "62429fd5f7db3912fea7a716",
itemName: "b",
totalQty: 70,
createAt: "2022-04-07T08:05:41.653+00:00",
},
{
createdFor: "62223a3f43e080995d5b46bc",
itemName: "c",
totalQty: 110,
createAt: "2022-04-09T08:05:41.653+00:00",
},
{
createdFor: "62223a3f43e080995d5b46bc",
itemName: "c",
totalQty: 30,
createAt: "2022-04-06T08:05:41.653+00:00",
},
{
createdFor: "62223a3f43e080995d5b46bc",
itemName: "c",
totalQty: 100,
createAt: "2022-12-30T08:05:41.653+00:00", //here this data excludes because out range of date
},
];
}
**
answer
**
{
result: [
{
createdFor: 62429fd5f7db3912fea7a716,
stockInfo: [
{ itemName: "a", totalQty: 100 },
{ itemName: "b", totalQty: 150 },
],
},
{
createdFor: 62223a3f43e080995d5b46bc,
stockInfo: [
{ itemName: "c", totalQty: 140 },
],
},
];
}
2
Answers
Question like thiis
First you can convert the date array to a suitable format as done below
Then you can use the following mongodb query
Here is the code on mongoplayground for reference
https://mongoplayground.net/p/KQHPBym_Z2P%5B%5D%5B1%5D