Mongodb 3.4 has introduced $facet
aggregation
which processes multiple aggregation pipelines within a single stage
on the same set of input documents.
Using $facet
and $group
you can find documents with $limit
and can get total count.
You can use below aggregation in mongodb 3.4
db.collection.aggregate([
{ "$facet": {
"totalData": [
{ "$match": { }},
{ "$skip": 10 },
{ "$limit": 10 }
],
"totalCount": [
{ "$group": {
"_id": null,
"count": { "$sum": 1 }
}}
]
}}
])
Even you can use $count
aggregation which has been introduced in mongodb 3.6.
You can use below aggregation in mongodb 3.6
db.collection.aggregate([
{ "$facet": {
"totalData": [
{ "$match": { }},
{ "$skip": 10 },
{ "$limit": 10 }
],
"totalCount": [
{ "$count": "count" }
]
}}
])
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…