我想从BatteryReport文档中选择今天的报告,还要检索电池信息。我使用汇总方法来获取此信息,但无法同时通过reortDate和batteryBankId来过滤我的文档。根据这些架构,我想通过第一个架构中的reportDate和第二个架构中的batteryBankId来过滤文档。
batteryReportSchema是:
{
"_id" : ObjectId("5d219603228e0235a8d0768c"),
"batteryId" : "1",
"batteryStatus" : "H",
"batteryVoltage" : "1.5",
"batteryTemperature" : "10",
"reportDate" : "07/07/2019",
"__v" : 0
}
batterySchema是:
{
"_id" : ObjectId("5d21fa28b4a2221ccc4e5f31"),
"batteryId" : "1",
"batteryBrand" : "sdf",
"batteryModel" : "sdfsdf",
"batteryProductDate" : "2019-07-07",
"batteryVoltage" : "123",
"batteryVoltageMin" : "123",
"batteryVoltageMax" : "12",
"batteryInternalResistance" : "21",
"batteryCapacity" : "12",
"batteryMinTemperature" : "123213",
"batteryMaxTemperature" : "123",
"batteryMaximumChargingCurrent" : "123",
"batteryCycleOfCharge" : "123",
"batteryBankId" : ObjectId("5d21f9e5b4a2221ccc4e5f2e"),
"__v" : 0
}
没有匹配过滤器:
BatteryReports.aggregate([
{
$lookup:
{
from: "batteryschemas",
localField: "batteryId",
foreignField: "batteryId",
as: "batteryReports_with_their_info"
}
}
], function (err, result) {
console.log(result);
if (err) {
resp.status(status.NOT_FOUND).json({'respond': err + ''});
} else {
resp.status(status.OK).json(result);
}
// console.log(result);
});
使用匹配过滤器:
BatteryReports.aggregate([
{
$lookup:
{
from: "batteryschemas",
localField: "batteryId",
foreignField: "batteryId",
as: "batteryReports_with_their_info"
}
}
,
{
$match: {
$and: [{"reportDate": moment().format('L')}]
}
}
,
{
$replaceRoot: {newRoot: {$mergeObjects: [{$arrayElemAt: ["$batteryReports_with_their_info", 0]}, "$$ROOT"]}}
}
,
{
$project: {
batteryReports_with_their_info: 0,
batteryBrand: 0,
batteryVoltage: 0,
batteryMaximumChargingCurrent: 0,
batteryCycleOfCharge: 0,
batteryModel: 0,
batteryProductDate: 0,
batteryInternalResistance: 0,
batteryCapacity: 0,
__v: 0
}
}
], function (err, result) {
console.log(result);
if (err) {
resp.status(status.NOT_FOUND).json({'respond': err + ''});
} else {
resp.status(status.OK).json(result);
}
// console.log(result);
});
没有匹配过滤器的方法的结果是:
[
{
"_id": "5d219603228e0235a8d0768c",
"batteryId": "1",
"batteryStatus": "H",
"batteryVoltage": "1.5",
"batteryTemperature": "10",
"reportDate": "07/07/2019",
"__v": 0,
"batteryReports_with_their_info": [
{
"_id": "5d21fa28b4a2221ccc4e5f31",
"batteryId": "1",
"batteryBrand": "sdf",
"batteryModel": "sdfsdf",
"batteryProductDate": "2019-07-07",
"batteryVoltage": "123",
"batteryVoltageMin": "123",
"batteryVoltageMax": "12",
"batteryInternalResistance": "21",
"batteryCapacity": "12",
"batteryMinTemperature": "123213",
"batteryMaxTemperature": "123",
"batteryMaximumChargingCurrent": "123",
"batteryCycleOfCharge": "123",
"batteryBankId": "5d21f9e5b4a2221ccc4e5f2e",
"__v": 0
}
]
},
{
"_id": "5d219603228e0235a8d0768d",
"batteryId": "2",
"batteryStatus": "H",
"batteryVoltage": "1.5",
"batteryTemperature": "10",
"reportDate": "07/07/2019",
"__v": 0,
"batteryReports_with_their_info": [
{
"_id": "5d21fa4cb4a2221ccc4e5f32",
"batteryId": "2",
"batteryBrand": "sdf",
"batteryModel": "sdfsdf",
"batteryProductDate": "2019-07-07",
"batteryVoltage": "123",
"batteryVoltageMin": "123",
"batteryVoltageMax": "12",
"batteryInternalResistance": "21",
"batteryCapacity": "12",
"batteryMinTemperature": "123213",
"batteryMaxTemperature": "123",
"batteryMaximumChargingCurrent": "123",
"batteryCycleOfCharge": "123",
"batteryBankId": "5d21fa00b4a2221ccc4e5f30",
"__v": 0
}
]
},
{
"_id": "5d219648228e0235a8d0768f",
"batteryId": "1",
"batteryStatus": "H",
"batteryVoltage": "1.5",
"batteryTemperature": "10",
"reportDate": "07/07/2019",
"__v": 0,
"batteryReports_with_their_info": [
{
"_id": "5d21fa28b4a2221ccc4e5f31",
"batteryId": "1",
"batteryBrand": "sdf",
"batteryModel": "sdfsdf",
"batteryProductDate": "2019-07-07",
"batteryVoltage": "123",
"batteryVoltageMin": "123",
"batteryVoltageMax": "12",
"batteryInternalResistance": "21",
"batteryCapacity": "12",
"batteryMinTemperature": "123213",
"batteryMaxTemperature": "123",
"batteryMaximumChargingCurrent": "123",
"batteryCycleOfCharge": "123",
"batteryBankId": "5d21f9e5b4a2221ccc4e5f2e",
"__v": 0
}
]
},
{
"_id": "5d219648228e0235a8d07690",
"batteryId": "2",
"batteryStatus": "H",
"batteryVoltage": "1.5",
"batteryTemperature": "10",
"reportDate": "07/07/2019",
"__v": 0,
"batteryReports_with_their_info": [
{
"_id": "5d21fa4cb4a2221ccc4e5f32",
"batteryId": "2",
"batteryBrand": "sdf",
"batteryModel": "sdfsdf",
"batteryProductDate": "2019-07-07",
"batteryVoltage": "123",
"batteryVoltageMin": "123",
"batteryVoltageMax": "12",
"batteryInternalResistance": "21",
"batteryCapacity": "12",
"batteryMinTemperature": "123213",
"batteryMaxTemperature": "123",
"batteryMaximumChargingCurrent": "123",
"batteryCycleOfCharge": "123",
"batteryBankId": "5d21fa00b4a2221ccc4e5f30",
"__v": 0
}
]
},
{
"_id": "5d219651228e0235a8d07692",
"batteryId": "2",
"batteryStatus": "H",
"batteryVoltage": "1.5",
"batteryTemperature": "10",
"reportDate": "07/07/2019",
"__v": 0,
"batteryReports_with_their_info": [
{
"_id": "5d21fa4cb4a2221ccc4e5f32",
"batteryId": "2",
"batteryBrand": "sdf",
"batteryModel": "sdfsdf",
"batteryProductDate": "2019-07-07",
"batteryVoltage": "123",
"batteryVoltageMin": "123",
"batteryVoltageMax": "12",
"batteryInternalResistance": "21",
"batteryCapacity": "12",
"batteryMinTemperature": "123213",
"batteryMaxTemperature": "123",
"batteryMaximumChargingCurrent": "123",
"batteryCycleOfCharge": "123",
"batteryBankId": "5d21fa00b4a2221ccc4e5f30",
"__v": 0
}
]
},
{
"_id": "5d219651228e0235a8d07693",
"batteryId": "1",
"batteryStatus": "H",
"batteryVoltage": "1.5",
"batteryTemperature": "10",
"reportDate": "07/07/2019",
"__v": 0,
"batteryReports_with_their_info": [
{
"_id": "5d21fa28b4a2221ccc4e5f31",
"batteryId": "1",
"batteryBrand": "sdf",
"batteryModel": "sdfsdf",
"batteryProductDate": "2019-07-07",
"batteryVoltage": "123",
"batteryVoltageMin": "123",
"batteryVoltageMax": "12",
"batteryInternalResistance": "21",
"batteryCapacity": "12",
"batteryMinTemperature": "123213",
"batteryMaxTemperature": "123",
"batteryMaximumChargingCurrent": "123",
"batteryCycleOfCharge": "123",
"batteryBankId": "5d21f9e5b4a2221ccc4e5f2e",
"__v": 0
}
]
},
{
"_id": "5d22d720899cba2cdc87be54",
"batteryId": "1",
"batteryStatus": "H",
"batteryVoltage": "4.5",
"batteryTemperature": "34",
"reportDate": "07/08/2019",
"__v": 0,
"batteryReports_with_their_info": [
{
"_id": "5d21fa28b4a2221ccc4e5f31",
"batteryId": "1",
"batteryBrand": "sdf",
"batteryModel": "sdfsdf",
"batteryProductDate": "2019-07-07",
"batteryVoltage": "123",
"batteryVoltageMin": "123",
"batteryVoltageMax": "12",
"batteryInternalResistance": "21",
"batteryCapacity": "12",
"batteryMinTemperature": "123213",
"batteryMaxTemperature": "123",
"batteryMaximumChargingCurrent": "123",
"batteryCycleOfCharge": "123",
"batteryBankId": "5d21f9e5b4a2221ccc4e5f2e",
"__v": 0
}
]
},
{
"_id": "5d22d720899cba2cdc87be55",
"batteryId": "2",
"batteryStatus": "H",
"batteryVoltage": "14.5",
"batteryTemperature": "60",
"reportDate": "07/08/2019",
"__v": 0,
"batteryReports_with_their_info": [
{
"_id": "5d21fa4cb4a2221ccc4e5f32",
"batteryId": "2",
"batteryBrand": "sdf",
"batteryModel": "sdfsdf",
"batteryProductDate": "2019-07-07",
"batteryVoltage": "123",
"batteryVoltageMin": "123",
"batteryVoltageMax": "12",
"batteryInternalResistance": "21",
"batteryCapacity": "12",
"batteryMinTemperature": "123213",
"batteryMaxTemperature": "123",
"batteryMaximumChargingCurrent": "123",
"batteryCycleOfCharge": "123",
"batteryBankId": "5d21fa00b4a2221ccc4e5f30",
"__v": 0
}
]
}
]
具有匹配过滤条件的结果:
[
{
"_id": "5d22d720899cba2cdc87be54",
"batteryId": "1",
"batteryVoltageMin": "123",
"batteryVoltageMax": "12",
"batteryMinTemperature": "123213",
"batteryMaxTemperature": "123",
"batteryBankId": "5d21f9e5b4a2221ccc4e5f2e",
"batteryStatus": "H",
"batteryTemperature": "34",
"reportDate": "07/08/2019"
},
{
"_id": "5d22d720899cba2cdc87be55",
"batteryId": "2",
"batteryVoltageMin": "123",
"batteryVoltageMax": "12",
"batteryMinTemperature": "123213",
"batteryMaxTemperature": "123",
"batteryBankId": "5d21fa00b4a2221ccc4e5f30",
"batteryStatus": "H",
"batteryTemperature": "60",
"reportDate": "07/08/2019"
}
]
但是我希望文档具有特殊的BatteryBankId,例如“ batteryBankId”:“ 5d21fa00b4a2221ccc4e5f30”,因此最终响应应为:
{
"_id": "5d22d720899cba2cdc87be55",
"batteryId": "2",
"batteryVoltageMin": "123",
"batteryVoltageMax": "12",
"batteryMinTemperature": "123213",
"batteryMaxTemperature": "123",
"batteryBankId": "5d21fa00b4a2221ccc4e5f30",
"batteryStatus": "H",
"batteryTemperature": "60",
"reportDate": "07/08/2019"
}