我编写了一种方法,该方法每月运行一次,并向人们发送进度报告。进行了聚合,现在我需要扩展更多的数据。
在$ profiles展开后,我尝试执行查找,但这会清空最终结果中的字段。
async function(req, res, next) {
const d = new Date();
d.setMonth(d.getMonth() - 1);
const result = await MemoryCard.aggregate([
{$match: {'updatedAt': {
$gte: d,
$lte: new Date(),
}}},
{
$lookup: {
from: 'users',
localField: 'owner',
foreignField: '_id',
as: 'owner',
},
},
{$unwind: '$owner'},
{$unwind: '$profiles'},
{$match: {
'progress.createdAt': {
$gte: d,
$lte: new Date(),
},
'progress.file_one': {$eq: true},
'progress.file_two': {$eq: true},
}},
{$unwind: '$progress'},
{
$group: {
'_id': '$_id',
'owner': {$first: '$owner'},
'progress': {$addToSet: '$progress'},
'profiles': {$last: '$profiles'},
},
},
{$unwind: '$profiles'},
{
'$project': {
'owner.email': '$owner.email',
'owner.username': '$owner.username',
'progress': '$progress',
'profile': '$profiles',
},
},
]);
以下结果有很多我不需要在配置文件对象中使用的数据。如何在汇总查询中过滤或取消选择这些字段?例如,我只需要$ lookup profile._id字段。我不需要其余的东西。
[
{
"_id": "5caf5225e7179a36ac34964f",
"owner": {
"email": "max@*****.nl",
"username": "Max"
},
"progress": [
{
"_id": "5caf4675999c0c14d0cda13e",
"tool": "5c7d4c9971338741c09c6c66",
"createdAt": "2019-04-11T13:51:49.352Z",
"updatedAt": "2019-04-11T13:55:17.527Z",
"file_two": true,
"file_one": true
}
],
"profile": {
"data": [
25,
25,
25,
25,
25
],
"tools": [
"5c7d4c9971338741c09c6c66",
"5c7d4c9971338741c09c6c6a",
"5c7d4c9971338741c09c6c69",
"5c7d4c9971338741c09c6c65",
"5c7d4c9971338741c09c6c67"
],
"_id": "5cd40f23bc30d7001730aee6",
"answers": [
{
"_id": "5cd40f23bc30d7001730aef9",
"subtheme": "5c7e8fe1c64c950450ada50e",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aef8",
"subtheme": "5c7e8fe1c64c950450ada50f",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aef7",
"subtheme": "5c7e8fe1c64c950450ada510",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aef6",
"subtheme": "5c7e8fe1c64c950450ada511",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aef5",
"subtheme": "5c7e8fe1c64c950450ada512",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aef4",
"subtheme": "5c7e8fe2c64c950450ada514",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aef3",
"subtheme": "5c7e8fe2c64c950450ada515",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aef2",
"subtheme": "5c7e8fe2c64c950450ada516",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aef1",
"subtheme": "5c7e8fe2c64c950450ada518",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aef0",
"subtheme": "5cc9ee132a945a0017498a0a",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aeef",
"subtheme": "5cc908e7de81ed00179a4706",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aeee",
"subtheme": "5c7e8fe3c64c950450ada51b",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aeed",
"subtheme": "5c7e8fe3c64c950450ada51d",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aeec",
"subtheme": "5cc9efad2a945a0017498a0b",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aeeb",
"subtheme": "5c7e8fe3c64c950450ada51e",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aeea",
"subtheme": "5c7e8fe3c64c950450ada520",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aee9",
"subtheme": "5c7e8fe3c64c950450ada521",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aee8",
"subtheme": "5c7e8fe3c64c950450ada522",
"answer": "disagree"
},
{
"_id": "5cd40f23bc30d7001730aee7",
"subtheme": "5c7e8fe4c64c950450ada523",
"answer": "disagree"
}
],
"profile_id": "5c864fd4f98eeb1afc9cc80e",
"createdAt": "2019-05-09T11:29:39.733Z",
"updatedAt": "2019-05-09T11:29:39.733Z"
}
}
]