【问题标题】: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.

【问题讨论】:

    标签: javascript node.js mongoose aggregate


    【解决方案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',
          },
        },
      ]);
    

    【讨论】:

      猜你喜欢
      • 2021-12-18
      • 2019-12-01
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多