0

I'm currently aggregation data from Mongo using Mongoose, my query looks as follows:

var query = [
  {
    "$match": {
    "searchId": {
        "$in": ["992a27743a6e0916e4f05d9f760065b6"]
      },
      "created_at": {
        "$gte": "2015-08-31T00:00:00.000Z",
        "$lte": "2015-09-02T15:59:59.999Z"
      },
      "language": {
        "$in": ["en"]
      }
    }
  }, {
    "$sort": {
      "created_at": -1
    }
  }, {
    "$skip": 0
  }, {
    "$limit": 10
  }, {
    "$project": {
      "id": "$id",
      "name": "$name",
      "created_at": "$meta.created_at"
    }
  }
]

Now, as you can see, I have a $skip and $limit for pagination in the frontend, but to fully support the frontend, I would need to return the maximum number of documents available.

Is there any way I can do this in the same query?

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
woutr_be
  • 9,532
  • 24
  • 79
  • 129
  • 1
    Not in a sensible way there isn't, so please don't even try. If you looked at your favorite SQL driven pagination library you will also see that "total results" will be run in a separate query, just like you should be doing here. The only way you get "totals" is by "stuffing everything into an array and counting the results" then unwinding said array, then filtering out the page that you want. Even if it didn't break ( which it likely will ) the BSON limit, it's not a very performant way of doing things. Two queries. – Blakes Seven Sep 02 '15 at 06:53
  • @BlakesSeven Thanks, that makes sense. – woutr_be Sep 02 '15 at 06:54

0 Answers0