- 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"}}}])
- 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"
}]
}]
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…