Mongo Aggregation multiple sorting stages (nodejs)

Mongo Aggregation multiple sorting stages (nodejs)

I am trying to build complex aggregation pipeline, which will fetch items from collection and sort them multiple times.

I am trying to build complex aggregation pipeline, which will fetch items from collection and sort them multiple times.

What I need is to: 1) get all users from collection 2) sort them by user-selected value (f.e. by amount of comments they left) 3) sort them by role (admin, moderator, basic-user)

Idea is to show admin user first even if some basic user have more comments, but if we have 2 admins - they would be sorted too.

Note that first $sort is to avoid unexpected sort+limit behavior, when items aren't fetched in constant order. Value of $skip is calculated while building pipeline depending on page number passed in API request.

For now I have something like this:

  { $match: <some condition> },
  { $sort: { _id: 1 } },
  { $sort: { countOfComment: -1 } },
  { $sort: { isAdmin: -1, isModerator: -1 } },
  { $skip: <page * 10> },
  { $limit: 10 }

If I got it right - it should: 1) get all users sorted by id, so there would be no duplications in different pages. this requires unique field to work 2) sort by amount of comments 3) sort by user role. It meant to save previous sorting results, so

- user with 4 comments
- moderator with 0 comments
- admin with 3 comments
- user with 2 comments
- admin with 1 comment

would become

- admin with 3 comments
- admin with 1 comment
- moderator with 0 comments
- user with 4 comments
- user with 2 comments

But it doesn't. Looks like each next $sort statement loses results of previous one. Any ideas how to reach desired behavior?

node.js mongodb

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Use Express.js, Node.js and MongoDB.js

In this post, I will show you how to use Express.js, Node.js and MongoDB.js. We will be creating a very simple Node application, that will allow users to input data that they want to store in a MongoDB database. It will also show all items that have been entered into the database.

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

Build a REST API using Node.js, Express.js, Mongoose.js and MongoDB

Node.js, Express.js, Mongoose.js, and MongoDB is a great combination for building easy and fast REST API. You will see how fast that combination than other existing frameworks because of Node.js is a packaged compilation of Google’s V8 JavaScript engine and it works on non-blocking and event-driven I/O. Express.js is a Javascript web server that has a complete function of web development including REST API.

Hands on with Node.Js Streams | Examples & Approach

The practical implications of having Streams in Node.js are vast. Nodejs Streams are a great way to handle data chunks and uncomplicate development.

Node.js Performance: Node.js vs. Io.js

You may already be aware that Raygun uses Node.JS for our API nodes that receive your precious crash reporting data (we also do node.js crash reporting if you’re interested). We’ve peaked in the past at more than 110,000 requests per second coming...