如何在mongodb中对重复数据实现分页?

时间:2018-10-09 13:51:20

标签: node.js mongodb

我有收藏名称推荐,用户在其中推荐论文问题和供稿。根据我的要求,如果我跟随10个人,那么我应该通过分页获取所有推荐数据。我使用的是最新的mongodb版本

recommended:[{
        "_id": "5bbc7c87a1d19614c093145c",
        "userId": "1",
        "recommended": "quest1",
        "recommendType": "questions",
        "createdAt": "2018-10-09T10:01:43.093Z",
        "updatedAt": "2018-10-09T10:01:43.093Z",
    },
    {
        "_id": "5bbc7c78a1d19614c093143a",
        "userId": "2",
        "recommended": "quest1",
        "recommendType": "questions",
        "createdAt": "2018-10-09T10:01:28.255Z",
        "updatedAt": "2018-10-09T10:01:28.255Z",
    },
    {
        "_id": "5bbc7c6fa1d19614c0931426",
        "userId": "3",
        "recommended": "quest2",
        "recommendType": "questions",
        "createdAt": "2018-10-09T10:01:19.428Z",
        "updatedAt": "2018-10-09T10:01:19.428Z",
    },
    {
        "_id": "5bbc7b64a1d19614c09311e7",
        "userId": "4",
        "recommended": "quest3",
        "recommendType": "paperupload",
        "createdAt": "2018-10-09T09:56:52.193Z",
        "updatedAt": "2018-10-09T09:56:52.193Z",
    },
    {
        "_id": "5bbc7a17bd1f151bccea1b24",
        "userId": "1",
        "recommended": "paper1",
        "recommendType": "paperupload",
        "createdAt": "2018-10-09T09:51:19.637Z",
        "updatedAt": "2018-10-09T09:51:19.637Z",
    },
    {
        "_id": "5bbc7a0ebd1f151bccea1b1e",
        "userId": "2",
        "recommended": "paper2",
        "recommendType": "paperupload",
        "createdAt": "2018-10-09T09:51:10.656Z",
        "updatedAt": "2018-10-09T09:51:10.656Z",
    },
    {
        "_id": "5bbc7a05bd1f151bccea1b16",
        "userId": "3",
        "recommended": "paper3",
        "recommendType": "paperupload",
        "createdAt": "2018-10-09T09:51:01.615Z",
        "updatedAt": "2018-10-09T09:51:01.615Z",
    },
    {
        "_id": "5bbc79f7bd1f151bccea1b0e",
        "userId": "4",
        "recommended": "paper1",
        "recommendType": "paperupload",
        "createdAt": "2018-10-09T09:50:47.956Z",
        "updatedAt": "2018-10-09T09:50:47.956Z",
    },
    {
        "_id": "5bbc79eebd1f151bccea1b08",
        "userId": "5",
        "recommended": "quest1",
        "recommendType": "questions",
        "createdAt": "2018-10-09T09:50:38.658Z",
        "updatedAt": "2018-10-09T09:50:38.658Z",
    },
    {
        "_id": "5bbc79e2bd1f151bccea1b00",
        "userId": "6",
        "recommended": "paper2",
        "recommendType": "paperupload",
        "createdAt": "2018-10-09T09:50:26.624Z",
        "updatedAt": "2018-10-09T09:50:26.624Z",
    },
    {
        "_id": "5bbc795bbd1f151bccea0a4f",
        "userId": "1",
        "recommended": "paper7",
        "recommendType": "paperupload",
        "createdAt": "2018-10-09T09:48:11.660Z",
        "updatedAt": "2018-10-09T09:48:11.660Z",
    },
    {
        "_id": "5bbc78f9bd1f151bcce9e9d7",
        "userId": "6",
        "recommended": "quest6",
        "recommendType": "paperupload",
        "createdAt": "2018-10-09T09:46:33.914Z",
        "updatedAt": "2018-10-09T09:46:33.914Z",
    },
    {
        "_id": "5bbc7865a93f7f0e54f63b97",
        "userId": "1",
        "recommended": "feed1",
        "recommendType": "feeds",
        "createdAt": "2018-10-09T09:44:05.961Z",
        "updatedAt": "2018-10-09T09:44:05.961Z",
    },
    {
        "_id": "5bbc785ca93f7f0e54f63b80",
        "userId": "2",
        "recommended": "feed2",
        "recommendType": "feeds",
        "createdAt": "2018-10-09T09:43:56.450Z",
        "updatedAt": "2018-10-09T09:43:56.450Z",
    },
    {
        "_id": "5bbc7854a93f7f0e54f63b70",
        "userId": "3",
        "recommended": "feed3",
        "recommendType": "feeds",
        "createdAt": "2018-10-09T09:43:48.634Z",
        "updatedAt": "2018-10-09T09:43:48.634Z",
    },
    {
        "_id": "5bbc784da93f7f0e54f63b61",
        "userId": "4",
        "recommended": "feed8",
        "recommendType": "feeds",
        "createdAt": "2018-10-09T09:43:41.458Z",
        "updatedAt": "2018-10-09T09:43:41.458Z",
    },
    {
        "_id": "5bbc7845a93f7f0e54f63b4c",
        "userId": "6",
        "recommended": "feed5",
        "recommendType": "feeds",
        "createdAt": "2018-10-09T09:43:33.330Z",
        "updatedAt": "2018-10-09T09:43:33.330Z",
    },
    {
        "_id": "5bbc7800a93f7f0e54f63ab0",
        "userId": "6",
        "recommended": "paper1",
        "recommendType": "papers",
        "createdAt": "2018-10-09T09:42:24.658Z",
        "updatedAt": "2018-10-09T09:42:24.658Z",
        "__v": 0
    },
    {
        "_id": "5bbc77f8a93f7f0e54f63a9f",
        "userId": "8",
        "recommended": "quest1",
        "recommendType": "questions",
        "createdAt": "2018-10-09T09:42:16.182Z",
        "updatedAt": "2018-10-09T09:42:16.182Z",
    },
    {
        "_id": "5bbc77eea93f7f0e54f63a8e",
        "userId": "5",
        "recommended": "feed1",
        "recommendType": "feeds",
        "createdAt": "2018-10-09T09:42:06.798Z",
        "updatedAt": "2018-10-09T09:42:06.798Z"
    }];

我关注7个人

var followedId=['1','2','3','4','5','6','7','8'];

我使用0-5分页的find查询来查询我的追随者推荐的数据,因此我将获得重复的具有相同任务,纸张或提要的文档的结果,因此我如何使我的每个分页结果都唯一且纸张或纸张相同该问题在每次分页后都不应出现两次(也就是说,我的结果集应始终被推荐为最新结果,并且每次分页后都不得重复)

1 个答案:

答案 0 :(得分:0)

Recommend.aggregate([
        {$match: {$and: [{userId: {$in: FollowedUsers}}]}},
        // { "$match": { "userId":{ "$in": FollowedUsers }}},
        { $sort: {"createdAt": -1 } },
        { $group: {
            _id: "$recommended",
            count:{$sum:1},
            recommendType: { "$first": "$recommendType" },
            rec_id:{"$first": "$_id"},
            createdAt:{"$first": "$createdAt"}

        }},
        { $sort: {count: -1 } },
        {$skip: page*pageSize},
        {$limit: pageSize}
        ])