我对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.
答案 0 :(得分:0)
因此,我通过意识到重复的条目是分开的并且必须分组来解决它的!在我的固定查询下面。
SELECT u.id,
COUNT(d.id) AS num_devices,
SUM(messages) AS num_messages,
MAX(most_recent) AS most_recent_message
FROM users u
JOIN devices d ON d.user_id = u.id
JOIN (SELECT device_id,
COUNT() AS messages,
MAX(TIME) AS most_recent_message
FROM messages
GROUP BY device_id) m ON m.device_id = d.id
GROUP BY u.id