Sample_mflix year vs released

hi! is there a way to sort all movies of sample_mflix by year/released? i mean, some documents have YEAR property, some have RELEASED
im working with node, express and mongoose
thank you!

Hello @mongu, Welcome to the MongoDB community forum,

No straight way, I would suggest you make a single property by combining both, for the temporary solution you can try the below aggregation query,

  • $addFields to add a new property finalYear
  • $ifNull will return one of the existing property’s value
  • $sort by finalYear order by ascending
  { "$addFields": { "finalYear": { "$ifNull": ["$year", "$released"] } } },
  { "$sort": { "finalYear": 1 } }

Note: this query will not use the index if provided in year/released, and is expensive in performance!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.