为啥我的聚合返回重复数据?

Posted

技术标签:

【中文标题】为啥我的聚合返回重复数据?【英文标题】:Why does my aggregate return duplicate data?为什么我的聚合返回重复数据? 【发布时间】: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.

【问题讨论】:

【参考方案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',
      ,
    ,
    
      '$project': 
        'owner.email': '$owner.email',
        'owner.username': '$owner.username',
        'progress': '$progress',
        'profile': '$profiles',
      ,
    ,
  ]);

【讨论】:

以上是关于为啥我的聚合返回重复数据?的主要内容,如果未能解决你的问题,请参考以下文章

查询没有重复和聚合函数或 GROUP BY 子句问题。 - 重复

为啥 SQL 强制我在 GROUP BY 子句中重复 SELECT 子句中的所有非聚合字段? [关闭]

为啥我的函数返回 Promise <pending> [重复]

为啥我的`train_test_split()`返回相同的样本[重复]

为啥我的 php 代码没有从数据库中获取多行 [重复]

MongoDB聚合查找和展开[重复]