如何减少聚合管道中的重复数据?

问题描述 投票:0回答:1

我有一个管道,对于我所需要的东西来说非常好......但我认为有一些冗余的数据可以从管道中删除。

预期的输出

这就是我想要的输出结果的样子

{
  "_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-paginat-v2中间件。

我也在使用mongoose-aggregate-paginat-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个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.