【发布时间】:2019-09-03 08:05:57
【问题描述】:
我对 mongo 聚合相当陌生,所以我的鼻子在 mongo 文档中试图弄清楚。越来越近了!我正在对我的数据集运行一些查询,以从保存数据文档中获取当前结果。但是我得到的响应是集合中第一个结果的副本。
const result = await MemoryCard.aggregate([
{
$lookup: {
from: 'users',
localField: 'owner',
foreignField: '_id',
as: 'owner',
},
},
{'$unwind': '$owner'},
{$unwind: '$progress'},
{$match: {
'progress.createdAt': {
$gte: d,
$lte: new Date(),
},
'progress.file_one': {$eq: true},
'progress.file_two': {$eq: true},
}},
{$unwind: '$profiles'},
{
'$project': {
'owner.email': 1,
'owner.username': 1,
'progress': 1,
'profiles': 1,
},
},
]);
在我返回的结果中,您可以看出第一位是重复的:
[
{
"_id": "5ca8a0bf6a45e10ad41a7fb3",
"owner": {
"email": "kevin2@s*******.com",
"username": "kevin"
},
"profiles": {
"data": [
100,
100,
84,
91,
100
],
"tools": [
"5c7d4c9971338741c09c6c68",
"5c7d4c9971338741c09c6c6b",
"5c7d4c9971338741c09c6c74",
"5c7d4c9971338741c09c6c73",
"5c7d4c9971338741c09c6c75"
],
"timestamp": "2019-04-06T12:51:07.151Z",
"_id": "5ca8a0bf6a45e10ad41a7fb4",
"profile_id": "5c864fd3f98eeb1afc9cc809",
"createdAt": "2019-04-06T12:51:11.471Z",
"updatedAt": "2019-04-06T12:51:11.471Z"
},
"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
}
},
{
"_id": "5ca8a0bf6a45e10ad41a7fb3",
"owner": {
"email": "kevin2@s********.com",
"username": "kevin"
},
"profiles": {
"data": [
100,
100,
84,
91,
100
],
"tools": [
"5c7d4c9971338741c09c6c68",
"5c7d4c9971338741c09c6c6b",
"5c7d4c9971338741c09c6c74",
"5c7d4c9971338741c09c6c73",
"5c7d4c9971338741c09c6c75"
],
"timestamp": "2019-04-06T12:51:07.151Z",
"_id": "5ca8a0bf6a45e10ad41a7fb4",
"profile_id": "5c864fd3f98eeb1afc9cc809",
"createdAt": "2019-01-06T12:51:11.471Z",
"updatedAt": "2019-01-06T12:51:11.471Z"
},
"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
}
}
]```
Where as i'm expecting multiple users.
【问题讨论】:
标签: javascript node.js mongoose aggregate