'Mongoose: find by slug $or id

i'm trying to route /:slug and the user can type the _id or a slug generated by slugify.

i'm trying this on the route controller:

const query = await Tour.find({
  $or: [{ _id: req.params.slug }, { slug: req.params.slug }]
});

but it doesn't work, i only manage to make it work when i do this:

 if (req.params.slug.includes('-')) {
  query = await Tour.find({ slug: req.params.slug });
} else {
  query = await Tour.findById(req.params.slug);
}

the question is: what am i doing wrong using $or operator? thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source