如何在 mongoose where 3 条件下对数据进行分组
how to group data in mongoose where 3 condition
如何使用一些预定义的条件对聚合猫鼬中的数据进行分组,
数据将根据以下 3 个条件进行分组
< 5 小时、5-9 小时、>9 小时。
按工作日分组的数据。
如果timeWorkDay数据小于5,则分组到timework5,如果数据大于5,小于9,则数据分组到timework59,如果大于9,则数据将分组在 timework9
示例数据
const data = [
{
"_id": "620f3f7a2148a8227855068f",
"timeWorkDay": 8,
"created_at": "2022-02-18T06:40:58.932Z",
"updated_at": "2022-02-18T07:54:59.331Z",
"__v": 0,
},
{
"_id": "6201e0e52faf8f0228dfad27",
"created_at": "2022-02-08T03:17:57.018Z",
"updated_at": "2022-02-08T03:17:57.018Z",
"__v": 0,
"timeWorkDay": 12,
},
{
"_id": "61f21be553b6de54a8ef4e0b",
"id_company": "61e00d1a144fa470f1870154",
"created_at": "2022-02-03T02:48:57.006Z",
"updated_at": "2022-02-01T02:04:29.778Z",
"__v": 0,
"timeWorkDay": 14,
},
{
"_id": "61fb45647dda6444afd10e2e",
"created_at": "2022-02-03T02:48:57.006Z",
"updated_at": "2022-02-22T08:10:39.775Z",
"__v": "2022-02-06T17:00:00.000Z",
"timeWorkDay": 5,
},
More...
]
和预期数据
const result = [
timeWork5:[
{
"_id": "61fb45647dda6444afd10e2e",
"created_at": "2022-02-03T02:48:57.006Z",
"updated_at": "2022-02-22T08:10:39.775Z",
"__v": "2022-02-06T17:00:00.000Z",
"timeWorkDay": 5,
},
More...
],
timeWork59: [
{
"_id": "620f3f7a2148a8227855068f",
"timeWorkDay": 8,
"created_at": "2022-02-18T06:40:58.932Z",
"updated_at": "2022-02-18T07:54:59.331Z",
"__v": 0,
},
More...
],
timeWork9: [
{
"_id": "6201e0e52faf8f0228dfad27",
"created_at": "2022-02-08T03:17:57.018Z",
"updated_at": "2022-02-08T03:17:57.018Z",
"__v": 0,
"timeWorkDay": 12,
},
{
"_id": "61f21be553b6de54a8ef4e0b",
"id_company": "61e00d1a144fa470f1870154",
"created_at": "2022-02-03T02:48:57.006Z",
"updated_at": "2022-02-01T02:04:29.778Z",
"__v": 0,
"timeWorkDay": 14,
},
More...
],
]
我尝试了几种方法,但没有得到我预期的结果
const execQuery: any = await EmployeeModel.aggregate([
{
$group: {
_id: {
timeWorkDay5: {
$cond: [
{$lte: ["$timeWorkDay", 4]}
]
},
timeWorkDay9: {
$cond: [
{$gte: ["$timeWorkDay", 7]}
]
},
},
createdAt: { $first: "$joinDate" },
count: { $sum: 1 }
}
},
{ $sort: { joinDate: -1 } }
]);
认为 $facet
符合您的要求。
const execQuery: any = await EmployeeModel.aggregate([
{
$facet: {
timeWorkDay5: [
{
$match: {
$expr: {
$lte: [
"$timeWorkDay",
4
]
}
}
}
],
timeWorkDay59: [
{
$match: {
$expr: {
$and: [
{
$gte: [
"$timeWorkDay",
5
]
},
{
$lt: [
"$timeWorkDay",
9
]
}
]
}
}
}
],
timeWorkDay9: [
{
$match: {
$expr: {
$gte: [
"$timeWorkDay",
9
]
}
}
}
]
}
}
])
如何使用一些预定义的条件对聚合猫鼬中的数据进行分组, 数据将根据以下 3 个条件进行分组 < 5 小时、5-9 小时、>9 小时。
按工作日分组的数据。 如果timeWorkDay数据小于5,则分组到timework5,如果数据大于5,小于9,则数据分组到timework59,如果大于9,则数据将分组在 timework9
示例数据
const data = [
{
"_id": "620f3f7a2148a8227855068f",
"timeWorkDay": 8,
"created_at": "2022-02-18T06:40:58.932Z",
"updated_at": "2022-02-18T07:54:59.331Z",
"__v": 0,
},
{
"_id": "6201e0e52faf8f0228dfad27",
"created_at": "2022-02-08T03:17:57.018Z",
"updated_at": "2022-02-08T03:17:57.018Z",
"__v": 0,
"timeWorkDay": 12,
},
{
"_id": "61f21be553b6de54a8ef4e0b",
"id_company": "61e00d1a144fa470f1870154",
"created_at": "2022-02-03T02:48:57.006Z",
"updated_at": "2022-02-01T02:04:29.778Z",
"__v": 0,
"timeWorkDay": 14,
},
{
"_id": "61fb45647dda6444afd10e2e",
"created_at": "2022-02-03T02:48:57.006Z",
"updated_at": "2022-02-22T08:10:39.775Z",
"__v": "2022-02-06T17:00:00.000Z",
"timeWorkDay": 5,
},
More...
]
和预期数据
const result = [
timeWork5:[
{
"_id": "61fb45647dda6444afd10e2e",
"created_at": "2022-02-03T02:48:57.006Z",
"updated_at": "2022-02-22T08:10:39.775Z",
"__v": "2022-02-06T17:00:00.000Z",
"timeWorkDay": 5,
},
More...
],
timeWork59: [
{
"_id": "620f3f7a2148a8227855068f",
"timeWorkDay": 8,
"created_at": "2022-02-18T06:40:58.932Z",
"updated_at": "2022-02-18T07:54:59.331Z",
"__v": 0,
},
More...
],
timeWork9: [
{
"_id": "6201e0e52faf8f0228dfad27",
"created_at": "2022-02-08T03:17:57.018Z",
"updated_at": "2022-02-08T03:17:57.018Z",
"__v": 0,
"timeWorkDay": 12,
},
{
"_id": "61f21be553b6de54a8ef4e0b",
"id_company": "61e00d1a144fa470f1870154",
"created_at": "2022-02-03T02:48:57.006Z",
"updated_at": "2022-02-01T02:04:29.778Z",
"__v": 0,
"timeWorkDay": 14,
},
More...
],
]
我尝试了几种方法,但没有得到我预期的结果
const execQuery: any = await EmployeeModel.aggregate([
{
$group: {
_id: {
timeWorkDay5: {
$cond: [
{$lte: ["$timeWorkDay", 4]}
]
},
timeWorkDay9: {
$cond: [
{$gte: ["$timeWorkDay", 7]}
]
},
},
createdAt: { $first: "$joinDate" },
count: { $sum: 1 }
}
},
{ $sort: { joinDate: -1 } }
]);
认为 $facet
符合您的要求。
const execQuery: any = await EmployeeModel.aggregate([
{
$facet: {
timeWorkDay5: [
{
$match: {
$expr: {
$lte: [
"$timeWorkDay",
4
]
}
}
}
],
timeWorkDay59: [
{
$match: {
$expr: {
$and: [
{
$gte: [
"$timeWorkDay",
5
]
},
{
$lt: [
"$timeWorkDay",
9
]
}
]
}
}
}
],
timeWorkDay9: [
{
$match: {
$expr: {
$gte: [
"$timeWorkDay",
9
]
}
}
}
]
}
}
])