我尝试从aggregate
、lookup
和一些cond
之后得到的数组中获取特定字段
下面你可以看到我的查询
const attendanceData = await User.aggregate([
{
$match: {
lastLocationId: Mongoose.Types.ObjectId(typeId),
isActive: true,
},
},
{
$project: {
_id: 1,
workerId: 1,
workerFirstName: 1,
workerSurname: 1,
},
},
{
$lookup: {
from: "attendances",
localField: "_id",
foreignField: "employeeId",
as: "attendances",
},
},
{
$set: {
attendances: {
$filter: {
input: "$attendances",
cond: {
$and: [
{
$gte: ["$$this.Date", new Date(fromDate)],
},
{
$lte: ["$$this.Date", new Date(toDate)],
},
{
$eq: ["$$this.createdAs", dataType],
},
{
$eq: ["$$this.status", true],
},
{
$eq: ["$$this.workerType", workerType],
},
],
},
},
},
},
},
{ $skip: 0 },
{ $limit: 10 },
]);
下面是我得到的响应数据
{
"attendanceSheet": [
{
"_id": "60dd77c14524e6c116e16aaa",
"workerFirstName": "MEGHRAJ",
"workerSurname": "JADHAV",
"workerId": "2036",
"attendances": [
{
"_id": "6130781085b5055a15c32f2u",
"workerId": "2036",
"workerFullName": "MEGHRAJ JADHAV",
"workerType": "Employee",
"Date": "2022-10-01T00:00:00.000Z",
"createdAs": "ABSENT"
},
{
"_id": "6130781085b5055a15c32f2u",
"workerId": "2036",
"workerFullName": "MEGHRAJ JADHAV",
"workerType": "Employee",
"Date": "2022-10-02T00:00:00.000Z",
"createdAs": "ABSENT"
}
]
},
{
"_id": "60dd77c24524e6c116e16c0f",
"workerFirstName": "SANJAY",
"workerSurname": "DUTTA",
"workerId": "2031",
"attendances": [
{
"_id": "6130781a85b5055a15c3455y",
"workerId": "2031",
"workerFullName": "SANJAY DUTTA",
"workerType": "Employee",
"Date": "2022-10-02T00:00:00.000Z",
"createdAs": "ABSENT"
}
]
}
]
}
但我希望数据像下面这样只有几个字段在不是每个字段
{
"attendanceSheet": [
{
"_id": "60dd77c14524e6c116e16aaa",
"workerFirstName": "MEGHRAJ",
"workerSurname": "JADHAV",
"workerId": "2036",
"attendances": [
{
"_id": "6130781085b5055a15c32f2u",
"Date": "2022-10-01T00:00:00.000Z",
"createdAs": "ABSENT"
},
{
"_id": "6130781085b5055a15c32f2u",
"Date": "2022-10-02T00:00:00.000Z",
"createdAs": "ABSENT"
}
]
},
{
"_id": "60dd77c24524e6c116e16c0f",
"workerFirstName": "SANJAY",
"workerSurname": "DUTTA",
"workerId": "2031",
"attendances": [
{
"_id": "6130781a85b5055a15c3455y",
"Date": "2022-10-02T00:00:00.000Z",
"createdAs": "ABSENT"
}
]
}
]
}
3条答案
按热度按时间s5a0g9ez1#
您可以通过将所有匹配放在
"$lookup"
"pipeline"
中来简化/重构您的聚合管道。在mongoplayground.net上试试。
r7knjye22#
从您所拥有的内容获取所请求的输出的一个选项是
$map
和$reduce
:了解它在playground example上的工作原理
zbdgwd5y3#
下面的修改对我很有效