0

I have collection something like below

[
  {
    _id: id1,
    user: user1,
    city: "test1"
  },
  {
    _id: id2,
    user: user2,
    city: "test1"
  },
  {
    _id: id3,
    user: user3,
    city: "test2"
  },
.....
]

I want get something like, further, I want to sort results based on _id etc.

[
  results: [
    {
      _id: id1,
      user: user1,
      city: "test1"
    },
    {
      _id: id1,
      user: user2,
      city: "test1"
    },
   ...
  ],
  totalResults: 220,
  pageNumber: 2
]

I am using moongoose as well in my project

  • Does this answer your question? [Implementing pagination in mongodb](https://stackoverflow.com/questions/28105009/implementing-pagination-in-mongodb) – whoami - fakeFaceTrueSoul Apr 10 '20 at 14:46
  • Nope.. this is about the cursor, I want to send additional details such and totalResullt count and current page no. etc along with results – Ankur Vishnoi Apr 10 '20 at 15:09
  • What do you mean by `totalResults` & `pageNumber` ? For pagination from code you would pass `n num of results wanted` & `nth page` to DB (You can get n returned though just to make sure when if you requested 20 but it has only 10)..!! Try this :: (https://stackoverflow.com/questions/24160037/skip-and-limit-in-aggregation-framework) – whoami - fakeFaceTrueSoul Apr 10 '20 at 15:13
  • Hi @whoami, By totalresults means, how many results we have, I want to send this info from server, as limit will show only that number of count. for example I have 72 results in query, and I can navigate all those result using limit and skip. But how will I know that we have 72 results, as limit of 10 will send only 10 results on query – Ankur Vishnoi Apr 10 '20 at 15:24
  • Ok if you need total count of docs in Coll & pagination, `$facet` will be the best choice.. – whoami - fakeFaceTrueSoul Apr 10 '20 at 15:39
  • @whoami, I already mentioned AZURE and AWS do not supports $facet. and my site is on azure As per below documented links, AZURE and AWS do not support $facet. https://learn.microsoft.com/es-es/azure/cosmos-db/mongodb-feature-support-36#aggregation-stages https://docs.aws.amazon.com/documentdb/latest/developerguide/mongo-apis.html#w144aac17c19b5b3 – Ankur Vishnoi Apr 10 '20 at 15:52
  • You might get it thru certain tricks in aggregation but doing this count thing & pagination both in a single go using aggregation without `$facet` might not be feasible, So you better split it into two calls `db.collection.count()` & pagination.. Or if your portal is in scratch stage try to switch your DB to mongoDB Atlas(Cloud based DB solution provided by mongo).. – whoami - fakeFaceTrueSoul Apr 10 '20 at 16:14
  • Could you please share an example ? – Ankur Vishnoi Apr 11 '20 at 12:29

1 Answers1

2
db.collection.aggregate([
  {
    "$match": {
      query
    }
  },
  {
    $project: {
      user: "$user"
    }
  },
  {
    "$sort": shortBy
  },
  {
    $group: {
      _id: null,
      postCount: {
        $sum: 1
      },
      results: {
        $push: '$$ROOT'
      }
    }
  },
  {
    $project: {
      total: '$postCount',
      results: {
        $slice: [
          '$results',
          skip,
          limit
        ]
      }
    }
  }
])