Mongodb priority based sorting

Hello Everyone,

We are working on a query for fetching the feed, that we usually see in a social media app.

The Logic we want to put there is, this would be the order

  • If the user is in the contact list then his post will be given Priority
  • if the user is someone you are following then, his post will be given 2nd priority
  • if the user is in my nearby location then his post will be given 3rd priority.

Also, time is also a critical aspect when calculating the priority,

for example, lets suppose the current time is 4 PM, someone from my contacts posted at 1 PM, lets call it post 1 & there is someone else in the following list who posted at 2 PM lets call it post 2, then we need the follower post to come first in the feed rather than the post 1

But if we are applying sorting based on contacts then it’s returning all the posts for my contacts first irrespective of the time they are posted on.

Is there any approach using which we can sort based on the combination or conditional-based sorting?

Please help

It seems to me that time is the only priority because the following

means to me that a more recent post always comes first no matter who sent it.

Can you give an example where a post with 1st Priority should be displayed before a post with 3rd priority that is more recent?

Let’s suppose 2 Posts,

  • Someone in your contacts (Priority 1) posted 2 hrs before the current time, let’s call it Post X
  • Someone in your nearby location(Priority 1) posted 1 hrs before the current time, let’s call it Post Y

then Post X should come first

That seems to contradict your first example.

This

seems to indicate that an older post from higher priority comes first despite the time.

While the following

seems to indicate that a more recent post comes first despite the priority.