Help needed with the mongo filter

Hi All ,

Need help

I’ve a document of the EPISODE content , where each episode will have the details of the series and season which it belongs to and the episode number .

Sample data
{
seriesRef :Series_1,
seasonnumber : 1,
episodenumber : 1,
“duration”:90
},
{
seriesRef :Series_1,
seasonnumber : 2,
episodenumber : 1,
“duration”:90
},
{
seriesRef :Series_2,
seasonnumber : 1,
episodenumber :1,
“duration”:90
}

If the user watches 1st series episode 1 and 2nd series episode 1 and after some percentage completion , i’m supposed to show the next episodes in that season/series .

individually i get the data

find filter = {“seriesRef”:“series_1”,"seasonnumber ":1,"episodenumber ":{“gt”:1} }
find filter = {“seriesRef”:“series_2”,"seasonnumber ":1,"episodenumber ":{“gt”:1} }

can i combine this filer to get the result of all the episodes across the series ?

If all you want to do is combine the above filters, then you can use the $or operator.

However, it’s unclear from the above what the structure of the user/user watches collection is. Can you elaborate? And is %completion not part of the criteria? Best to explain the whole thing in a bit more detail.

Hi ,
Sorry for the delay in reply .

Yeah we do not have to consider the percentage of completion in the query criteria.

I’m just looking to find the next episodes in the season , given the current episode details .
(i.e I would have the list details of series/season/episode being watched )

find filter = {“seriesRef”:“series_1”,"seasonnumber ":1,"episodenumber ":{“gt”:“CURRENT_EPSISODE”} }

So in order to find the next episodes of multiple series which i would have , should i do multiple reads or can i find all the required data at once .

Hope its bit clear now .

Thank you .

No problem there @ramya_80655 :+1:

Sure! Here are the steps for the aggregation pipeline:

  1. Use an $addFields stage to create a field that will be a concatenation of seasonnumber and episodenumber.
    I.e. $convert both fields to string, concatenate them $concat: [seasonnumber, episodenumber], and then $convert the result to an integer.
    So seasonnumber: 1 and episodenumber: 2, will become 12.
  2. Perform a $match using the concatenated field from step 1. E.g.
   new_concatenated_field: {$gt: "seasonnumber + episodenumber" }

In the future, consider creating a sequence field instead which will start from 1 and incremented by 1 for each season/episode combination in order. That way you wan use this field for this style of searching.

1 Like