The title of the question summarises my current approach in trying to do query that tries to achieve the following:
"Find all documents that match the text search criteria OR don't AND fulfil a bunch of other properties"
My query is below:
var query = {
$or: [
{ $text: { $search: searchText } },
],
$and: [
{createdon: { $gte: start_date, $lt: end_date} },
{author: req.user._id}
]
};
If an empty string is sent by the user the query doesn't return any results although there are documents in the collection that fit the criteria sent in the request by the user inside the AND clause.
So basically I'm scratching my head as to how to get the $text search of MongoDB to work with an empty string.
I know I could simply text before creating the query, something like so:
var query;
if (!searchText.length) {
//We know there's no search text, so just use the conditions in the AND clause
query = {
createdon: { $gte: start_date, $lt: end_date},
author: req.user._id
};
Just wondering If i can get this desired functionality without having to create the query object inside the control flow of a conditional.