Mongodb dynamic Ranking / Sorting?

Suppose my data returns like this form :-

 [
  {
    "_id": "611bc112431d9c0b44161968",
    "title": "Nillkin Case for Apple iPhone 7 / Apple iPhone SE2 SE 2 iPhone SE 2020 (4.7\" Inch) Sparkle Leather Flip Folio Book Type PC Gold Color  ",
    "price": 353585
  },
  {
    "_id": "611bc112431d9c0b4416198d",
    "title": "Nillkin Case for Apple iPhone 8 / Apple iPhone SE2 SE 2 iPhone SE 2020 (4.7\" Inch) Sparkle Leather Flip Folio Book Type PC Gold Color  ",
    "price": 13255
  },
  {
    "_id": "611bc112431d9c0b44161937",
    "title": "Apple iPhone XR (64GB) - Black  ",
    "price": 40000
  },
  {
    "_id": "611bc112431d9c0b44161992",
    "title": "New Apple iPhone 12 (128GB) - Blue with Apple Clear Case with Magsafe (for iPhone 12, 12 Pro)  ",
    "price": 12000
  },
  {
    "_id": "611bc112431d9c0b441619d5",
    "title": "New Apple iPhone 12 (128GB) - Green with Apple Clear Case with Magsafe (for iPhone 12, 12 Pro)  ",
    "price": 40000,
  },
  {
    "_id": "611bc112431d9c0b441619ed",
    "title": "New Apple iPhone 12 (128GB) - Blue with Apple Clear Case with Magsafe (for iPhone 12, 12 Pro)  ",
    "price": 15680,
  },
  {
    "_id": "611bc112431d9c0b441619f3",
    "title": "New Apple iPhone 12 (256GB) - Black with Apple Clear Case with Magsafe (for iPhone 12, 12 Pro)  ",
    "price": 60000,
  },
  {
    "_id": "611bc112431d9c0b44161a33",
    "title": "New Apple iPhone 12 (64GB) - Black with Apple Clear Case with Magsafe (for iPhone 12, 12 Pro)  ",
    "price": 432525,
    
  },
  {
    "_id": "611bc112431d9c0b44161a37",
    "title": "New Apple iPhone 12 (64GB) - Blue with Apple Clear Case with Magsafe (for iPhone 12, 12 Pro)  ",
    "price": 55000,
  },
  {
    "_id": "611bc112431d9c0b44161a4c",
    "title": "New Apple iPhone 12 (128GB) - Black with Apple Clear Case with Magsafe (for iPhone 12, 12 Pro)  ",
    "price": 2244,
  },
  {
    "_id": "611bc112431d9c0b4416192c",
    "title": "New Apple iPhone 11 (128GB) - Black  ",
    "price": 13255,
  },
  {
    "_id": "611bc112431d9c0b4416192d",
    "title": "New Apple iPhone 12 (128GB) - Green  ",
    "price": 432525,
  },
  {
    "_id": "611bc112431d9c0b4416192e",
    "title": "New Apple iPhone 11 (64GB) - Black  ",
    "price": 34342,
  },
  {
    "_id": "611bc112431d9c0b4416192f",
    "title": "New Apple iPhone 12 (128GB) - Blue  ",
    "price": 353585,
  },
  {
    "_id": "611bc112431d9c0b44161930",
    "title": "New Apple iPhone 11 (64GB) - Purple  ",
    "price": 66430, 
  }
]

I know very well that if I use $sort then its arranges the data format in ascending and descing form.

but I want my data returned like this: (DYNAMICALLY RANKING)

[
  {
    "_id": "611bc112431d9c0b44161968",
    "title": "Nillkin Case for Apple iPhone 7 / Apple iPhone SE2 SE 2 iPhone SE 2020 (4.7\" Inch) Sparkle Leather Flip Folio Book Type PC Gold Color  ",
    "price": 9000
  },
  {
    "_id": "611bc112431d9c0b4416198d",
    "title": "Nillkin Case for Apple iPhone 8 / Apple iPhone SE2 SE 2 iPhone SE 2020 (4.7\" Inch) Sparkle Leather Flip Folio Book Type PC Gold Color  ",
    "price": 8000
  },
  {
    "_id": "611bc112431d9c0b44161937",
    "title": "Apple iPhone XR (64GB) - Black  ",
    "price": 12000
  },
  {
    "_id": "611bc112431d9c0b44161992",
    "title": "New Apple iPhone 12 (128GB) - Blue with Apple Clear Case with Magsafe (for iPhone 12, 12 Pro)  ",
    "price": 8500
  }
]

My aggeration

Pipeline = [{
                        '$search': {
                            'text': {
                                'query': `Iphone`,
                                'path': 'title',
                            }
                        }
                    } ]

Now, Any one please Suggest me the solution guys :pray: :pray:

Despite the given examples, it is not clear to me what do you mean by

A little bit more description of the requirements would help us to help you.

hi, I want to rank result dynamically. not increase or neither decreasing. means If Higher Price should rank on Top and lower price should rank on down.

but $sort directly arranges into ascending or descending format. take example at amazon ecommerce results.

That doesn’t help. Given ten values, how should they be sorted? If you cannot explain it to us, I don’t think there is a way to “explain” it to the $sort function either!

Now, your aggregation is using $search aka Atlas Search with Lucene. Do you want things ranked based on how closely they match the search or strictly by price or some combination of things?

Asya

2 Likes

yes, I think You understand what I want to achieve.

Here’s the sample data of 100 documents. I set aggeration to retrieve results 10 results per page.

This data price range varies from 1,000 to 100,000. It’s not in USD.

and Yes, I do use $search which returns results based on text matching. and it doesn’t sort the results based on price.

So, I want to sort those results in Descending form. Higher Price ranks at First and Lower Price ranks at the bottom and it can be accessed on the last page. like in page 9 or 10 (10page per results) Also be noted that I don’t want it to arrange very strictly. Because I’ve already set up Sorting in ascending and descending for doing that.

It’s same like the other ecommerce websites. Let’s suppose if we search for Mobile, then it retrives and shows result on top which has highest no. of buys or Clicks or Price, But it doesn’t it very strictly. I’m trying to build very similar to that.

Aggeration For performing Searches:

        const Pipeline = [{
                        '$search': {
                            'index': 'default',
                            'text': {
                                'query': `query`,
                                'path': 'name',
                            }
                        }
                    },{ '$limit': 10 }];

I hope that you understand @Asya_Kamsky

When you say you don’t want a strict order, I’m afraid that’s without calculating some sort of “approximate” ranking there is no way to use $sort - the closest I can see if maybe computing based on some formula a “sort score” which takes into account relevance from first search combined with order of magnitude of price or something like that… then $sort on the calculated score (without showing it).