【问题标题】:How can I reduce the duplicate data from my aggregation pipeline?如何减少聚合管道中的重复数据?
【发布时间】:2020-09-20 09:20:31
【问题描述】:

我有一个非常适合我需要的管道...但我认为可以从管道中删除一些冗余数据。

预期输出

这就是我希望输出的样子

{
  "_id": "5ecee2189fdd1b0004056936",
  "name": "Mike",
  "history": [
    {
        "_id": "5ecb263c166b8500047c1411",
        "what": "Log IN"
    },
    {
        "_id": "5ecb263c166b8500047c1422",
        "what": "Log OUT"
    }
  ]
}

电流输出

这是当前输出的样子

{
  "docs": [
    {
      "_id": "5ecee2189fdd1b0004056936",
      "name": "Mike",
      "history": {
        "_id": "5ecb263c166b8500047c1411",
        "what": "Log IN"
      },
      "historyIndex": 0
    },
    {
      "_id": "5ecee2189fdd1b0004056936",
      "name": "Mike",
      "history": {
        "_id": "5ecb263c166b8500047c1422",
        "what": "Log OUT"
      },
      "historyIndex": 1
    }
  ]
}

用户文档

在现实生活中,用户会比这更多……当然……

{
  "_id": "5ecee2189fdd1b0004056936",
  "name": "Mike",
}

历史文档

再次,为了简单起见,我保持数据简短

[
  {
    "_id": "5ecb263c166b8500047c1411",
    "userId": "5ecee2189fdd1b0004056936",
    "what": "Log IN"
  },
  {
    "_id": "5ecb263c166b8500047c1422",
    "userId": "5ecee2189fdd1b0004056999",
    "what": "Log IN"
  },
  {
    "_id": "5ecb263c166b8500047c1433",
    "userId": "5ecee2189fdd1b0004056936",
    "what": "Log OUT"
  },
  {
    "_id": "5ecb263c166b8500047c1444",
    "userId": "5ecee2189fdd1b0004056999",
    "what": "Log OUT"
  }
]

mongoose-aggregate-paginate-v2 中间件

我也在使用 mongoose-aggregate-paginate-v2,但我不认为这是我的问题,但是当返回结果时它肯定会发挥作用。它需要将文档展平,以便对它们进行计数和分页:

    "totalDocs": 941,
    "limit": 500,
    "page": 1,
    "totalPages": 2,
    "pagingCounter": 1,
    "hasPrevPage": false,
    "hasNextPage": true,
    "prevPage": null,
    "nextPage": 2

管道

这是我的管道

        var agg_match = {
            $match: 
            {
                _id: mongoose.Types.ObjectId(userId)
            }
        };

        var agg_lookup = {
            $lookup: {
                from: 'it_userhistories',
                localField: '_id',
                foreignField: 'userId',
                as: 'history'
            }
        }

        var agg_unwind = {
            $unwind: {
                path: "$history",
                preserveNullAndEmptyArrays: true,
                includeArrayIndex: 'historyIndex',
            }
        }

        var agg = [
            agg_match,
            agg_lookup,
            agg_unwind,
            agg_project,
        ];


        var pageAndLimit = {
            page:page,
            limit:limit
        }


       User.aggregatePaginate(myAggregate, pageAndLimit)

【问题讨论】:

    标签: mongodb pipeline projection


    【解决方案1】:

    您可以使用$map 运算符来执行此操作。以下查询会有所帮助(我没有在管道中包含匹配阶段,您可以轻松包含它):

    db.user.aggregate([
      {
        $lookup: {
          from: "history",
          localField: "_id",
          foreignField: "userId",
          as: "history"
        }
      },
      {
        $project: {
          name: 1,
          history: {
            $map: {
              input: "$history",
              as: "h",
              in: {
                _id: "$$h._id",
                what: "$$h.what"
              }
            }
          }
        }
      }
    ])
    

    MongoPLayGroundLink

    【讨论】:

    • 完美!谢谢!
    • 我的问题怎么样?可以理解吗?如果可以,你能给它投票吗?谢谢
    猜你喜欢
    • 2020-08-20
    • 2016-10-17
    • 2020-01-11
    • 2015-05-01
    • 1970-01-01
    • 2019-12-22
    • 2019-09-21
    • 2019-01-04
    • 2017-01-31
    相关资源
    最近更新 更多