Elasticsearch pinned query MongoDB equivalent exists?

Let’s imagine I do have a collection of movies. Which have a tag with the movie genre.

I would like to find movies for the genre Horror and Comedy. However, I would like to prepend to find result 5 featured movies which are might not be Horror and Comedy.

So my collection will look like…

[featured1, featured2, featured3, Horror, Comedy, Horror, Horror...]

I know the IDS of featured movies and I would like the order of featured movies to be defined by the order of ids in the search query however the rest of the movies should be ordered by title.

At this moment I’m making to finds and I’m contacting results however this solution required a bunch of code to handle pagination correctly.

Is there any chance I can get this result in one find operation or somehow concat results of 2 find queries and get pagination not broken?

I’m using rustlang in the backend :slight_smile:

Looks like elasticsearch have something like thishttps://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-pinned-query.html

Is the same thing possible in MongoDB?

Hi @Mateusz_Spiewak ,

First maybe you can use 2 quaries in a $unionWith aggregation, first for the featured movies and second for the other search:

Now to get items by an array order you can see an aggregation I helped with here :

The second union part sort by title in its pipeline.

Let me know if that makes sense.


Hello @Pavel_Duchovny!

Thank you… I will take a look and let you know :slight_smile: