I am trying to query an array of cars. This is an example of my data set:
[
{
"model": "Model 3",
"location": "Canada",
"color": "green",
"engine": "1.4 turbo",
"gearbox": "automatic",
...
},
{
"model": "Model 4",
"location": "Brazil",
"color": "green",
"engine": "1.4 turbo",
"gearbox": "automatic",
...
}
]
This is my query:
Car.paginate(
{
model: new RegExp(`^${req.body.model}$`, 'i'),
color: new RegExp(`^${req.body.color}$`, 'i'),
...
},
{ offset, limit, sort: getSort(req.query.sortType), populate: ['car'] }
)
This query is using an implicit $and but that's not what I want to do. I want to return all the documents that satisfy one or more of the query but also that takes into account all queries. So, if I search for "model 1" it will return all cars that are named "model 1" and if I search for "model 1" and "black" it should return all cars that are named "model 1" and are of the color "black" (NOT all "model 1" AND all color "black").
Also, the way I am doing it right now, all fields are required to exist in my req.body. I would like for it to not require all the fields, only search with the fields it's given.