I have a Product collection in mongoDB and I use mongoose. What am trying to achieve is, to search products by name
I have to search for the term "Los Angeles Lakers"
const results = await Products.find({
$or: [
{ name: { $regex: "Los", $options: "i" } },
{ name: { $regex: "Angeles", $options: "i" } },
{ name: { $regex: "Lakers", $options: "i" } },
],
});
This works fine. But I want to sort the results like, the search results of "Los" first, results of "Angeles" then, and the results of "Lakers" after that.
Also is there a way to accompish the above task similar to this
const results = await Products.find({
$or: [
{ name: { $regex: $or: ["Los", "Angeles", "Lakers"], $options: "i" } }
],
});
This is my first stack overflow question so bare with me if this is long or not formatted right. Thanks in advance!