Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

pymongo - How to paginate on array of array with using $skip and $limit in mongodb?

  1. The pagination works on "records" with the following query for the defined document in MongoDB by changing $skip and $limit value.

Document in a collection:

{
    "_id": ObjectId('5fc9cfbbc6f80cfc66c7ed0e'),
    "created_by": "admin",
    "records": [{
        "first_name": "first_name1",
        "last_name": "last_name1"
    }, {
        "first_name": "first_name2",
        "last_name": "last_name2"
    }, {
        "first_name": "first_name3",
        "last_name": "last_name3"
    }]
}

Aggregate pipeline to paginate:

db.collection.aggregate([
    {"$match": {"_id": ObjectId('5fc9cfbbc6f80cfc66c7ed0e')}},
    {"$unwind": "$records"},
    {"$skip": 0},
    {"$limit": 2},
    {"$group": {"_id": "$_id", "records": {"$push": "$records"}}}])
  1. How to use the aggregate pipeline and paginate on "records" with $skip and $limit for the following document structure?

Document in a collection:

{
    "_id": ObjectId('5fc9cfbbc6f80cfc66c7ed0e'),
    "created_by": "admin",
    "matched_records": [{
        "_id": 1,
        "from": "A",
        "records": [{
            "first_name": "first_name11",
            "last_name": "last_name11"
        }, {
            "first_name": "first_name12",
            "last_name": "last_name12"
        }, {
            "first_name": "first_name13",
            "last_name": "last_name13"
        }]
    }, {
        "_id": 2,
        "from": "B",
        "records": [{
            "first_name": "first_name21",
            "last_name": "last_name21"
        }, {
            "first_name": "first_name22",
            "last_name": "last_name22"
        }, {
            "first_name": "first_name23",
            "last_name": "last_name23"
        }]
    }, {
        "_id": 3,
        "from": "C",
        "records": [{
            "first_name": "first_name31",
            "last_name": "last_name31"
        }, {
            "first_name": "first_name32",
            "last_name": "last_name32"
        }, {
            "first_name": "first_name33",
            "last_name": "last_name33"
        }]
    }]
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...