按选定的日期范围和来自 mongodb 和 nodejs 的 createdFor 对数据进行排序
sort data by selected range of date and createdFor from mongodb and nodejs
我想根据日期和 createdFor
属性 对数据进行排序。我想要 createdFor
组中的数据总和以及每个 createdFor
.
的所有特定 itemName
日期 = [2022 年 4 月 5 日星期二 11:10:41 GMT-0400(东部夏令时间),2022 年 4 月 9 日星期六 11:10:41 GMT-0400(东部夏令时间)]
createdfor 可以重复,所有 createdFor 的 itemName 都不同
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
},
];
}
**
回答
**
{
result: [
{
createdFor: 62429fd5f7db3912fea7a716,
stockInfo: [
{ itemName: "a", totalQty: 100 },
{ itemName: "b", totalQty: 150 },
],
},
{
createdFor: 62223a3f43e080995d5b46bc,
stockInfo: [
{ itemName: "c", totalQty: 140 },
],
},
];
}
首先,您可以将日期数组转换为合适的格式,如下所示
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)']
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
new_dates = []
date.forEach((d) => {
str = d.split(' ')
mon = ('0'+(months.indexOf(str[1])+1)).slice(-2)
new_dates.push(str[3]+'-'+mon+'-'+str[2])
})
然后你可以使用下面的mongodb查询
db.collection.aggregate([
{
"$project": {
createdFor: 1,
itemName: 1,
totalQty: 1,
createAt: {
"$substr": [
"$createAt",
0,
10
]
}
}
},
{
"$match": {
"$and": [
{
createAt: {
"$gte": "2022-04-05"
}
},
{
createAt: {
"$lte": "2022-04-09"
}
}
]
}
},
{
"$group": {
"_id": {
for: "$createdFor",
name: "$itemName"
},
"sum": {
"$sum": "$totalQty"
}
}
},
{
"$group": {
"_id": "$_id.for",
"stockInfo": {
"$push": {
itemName: "$_id.name",
totalQty: "$sum"
}
}
}
}
])
这里是mongoplayground上的代码,供参考
这样的问题
[
{
createdFor: "62429fd5f7db3912fea7a716",
stockOutDetail: {
stock_name: "a",
totalQty: 110
},
createAt: "2022-04-05T08:05:41.653+00:00",
},
{
createdFor: "62429fd5f7db3912fea7a716",
stockOutDetail: {
stock_name: "b",
totalQty: 50
},
createAt: "2022-04-08T08:05:41.653+00:00",
},
{
createdFor: "62429fd5f7db3912fea7a716",
stockOutDetail: {
stock_name: "b",
totalQty: 70
},
createAt: "2022-04-07T08:05:41.653+00:00",
},
{
createdFor: "62223a3f43e080995d5b46bc",
stockOutDetail: {
stock_name: "c",
totalQty: 70
},
createAt: "2022-04-09T08:05:41.653+00:00",
},
{
createdFor: "62223a3f43e080995d5b46bc",
stockOutDetail: {
stock_name: "c",
totalQty: 70
},
createAt: "2022-04-06T08:05:41.653+00:00",
},
{
createdFor: "62223a3f43e080995d5b46bc",
stockOutDetail: {
stock_name: "c",
totalQty: 70
},
createAt: "2022-12-30T08:05:41.653+00:00",
//here this data excludes because out range of date
},
]
我想根据日期和 createdFor
属性 对数据进行排序。我想要 createdFor
组中的数据总和以及每个 createdFor
.
itemName
日期 = [2022 年 4 月 5 日星期二 11:10:41 GMT-0400(东部夏令时间),2022 年 4 月 9 日星期六 11:10:41 GMT-0400(东部夏令时间)]
createdfor 可以重复,所有 createdFor 的 itemName 都不同
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
},
];
}
** 回答 **
{
result: [
{
createdFor: 62429fd5f7db3912fea7a716,
stockInfo: [
{ itemName: "a", totalQty: 100 },
{ itemName: "b", totalQty: 150 },
],
},
{
createdFor: 62223a3f43e080995d5b46bc,
stockInfo: [
{ itemName: "c", totalQty: 140 },
],
},
];
}
首先,您可以将日期数组转换为合适的格式,如下所示
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)']
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
new_dates = []
date.forEach((d) => {
str = d.split(' ')
mon = ('0'+(months.indexOf(str[1])+1)).slice(-2)
new_dates.push(str[3]+'-'+mon+'-'+str[2])
})
然后你可以使用下面的mongodb查询
db.collection.aggregate([
{
"$project": {
createdFor: 1,
itemName: 1,
totalQty: 1,
createAt: {
"$substr": [
"$createAt",
0,
10
]
}
}
},
{
"$match": {
"$and": [
{
createAt: {
"$gte": "2022-04-05"
}
},
{
createAt: {
"$lte": "2022-04-09"
}
}
]
}
},
{
"$group": {
"_id": {
for: "$createdFor",
name: "$itemName"
},
"sum": {
"$sum": "$totalQty"
}
}
},
{
"$group": {
"_id": "$_id.for",
"stockInfo": {
"$push": {
itemName: "$_id.name",
totalQty: "$sum"
}
}
}
}
])
这里是mongoplayground上的代码,供参考
这样的问题
[
{
createdFor: "62429fd5f7db3912fea7a716",
stockOutDetail: {
stock_name: "a",
totalQty: 110
},
createAt: "2022-04-05T08:05:41.653+00:00",
},
{
createdFor: "62429fd5f7db3912fea7a716",
stockOutDetail: {
stock_name: "b",
totalQty: 50
},
createAt: "2022-04-08T08:05:41.653+00:00",
},
{
createdFor: "62429fd5f7db3912fea7a716",
stockOutDetail: {
stock_name: "b",
totalQty: 70
},
createAt: "2022-04-07T08:05:41.653+00:00",
},
{
createdFor: "62223a3f43e080995d5b46bc",
stockOutDetail: {
stock_name: "c",
totalQty: 70
},
createAt: "2022-04-09T08:05:41.653+00:00",
},
{
createdFor: "62223a3f43e080995d5b46bc",
stockOutDetail: {
stock_name: "c",
totalQty: 70
},
createAt: "2022-04-06T08:05:41.653+00:00",
},
{
createdFor: "62223a3f43e080995d5b46bc",
stockOutDetail: {
stock_name: "c",
totalQty: 70
},
createAt: "2022-12-30T08:05:41.653+00:00",
//here this data excludes because out range of date
},
]