$ lookup的分页? MongoDB的

时间:2018-06-05 22:32:10

标签: mongodb pagination aggregation

-

嗨,我想分页我的$ lookup结果。我怎么能这样做?

我当前的查询。

var pipeline = [{
                $match: {
                    "user": user_id
                }
            },
            {
                $lookup: {
                    "from": "users",
                    "localField": "followers",
                    "foreignField": "user",
                    "as": "followers"
                }
            },{
                $project: {
                    "user": 1,
                    "followers": 1
                }
            }
        ]
//Aggregation function
let followers = await Follow.aggregate(pipeline).allowDiskUse(true).cursor({ batchSize: 9}).exec().toArray()

我的查询结果(API)。

{
"code": 200,
"data": [
    {
        "_id": "5b15a8839a44b13ba9a2bb64",
        "user": "5b09932c9978bd0ca85037bf",
        "followers": [
            {
                "_id": "5b06f9b7a494db2c78ff7ad7",
                "updatedAt": "2018-05-24T17:43:19.997Z",
                "createdAt": "2018-05-24T17:43:19.997Z",
                "email": "snow@gmail.com",
                "password": "$2a$10$oB83NnSc4vhPprOZtsJqrO/KAI8XWstp82DpB9mxEX6UB1jo3qgoO",
                "role": "user",
                "__v": 0
            },
            {
                "_id": "5b09932c9978bd0ca85037bf",
                "updatedAt": "2018-05-26T17:02:36.790Z",
                "createdAt": "2018-05-26T17:02:36.790Z",
                "email": "bread@gmail.com",
                "password": "$2a$10$eXbv/QYdfL.8TDWRnqLyZ.vPfbEqh6KvEVhe6n7nj5dwAizzHlWnm",
                "role": "user",
                "__v": 0
            }
        ]
    }
]}

所以我想说我只想从2中获得1个关注者。那么,有没有办法放置页面并限制$ lookup查询?

-

UPD#1。我没有看到$ lookup数组的任何“分页”提示,主数组只有“分页”而不是$ lookup字段。

UPD#2。我发现你正在对主要结果进行分页而不是基于关系的结果。

0 个答案:

没有答案