Searching for a way to enhance my case-insensitive search method across five different collections, specifically targeting the title
field. I am also looking to retrieve partial results with at least three characters.
For Example:
// Collection 1
{ title: 'Sample' },
{ title: 'Another sample' }
{ title: 'This is an example' }
// Collection 2
{ title: 'Something else' },
{ title: 'A sample document' }
{ title: 'This is another example' }
Ample
: All documents except the first one of the second collectionSample
: The first two documents of collection 1 and the second document of collection 2another
: Second document of collection 1is
: Should not give any result (less than 3 characters)
I have been using the following method:
db.collection.find({ title: new RegExp(value, 'i') }).fetch()
...for each collection and then combining the results into one array. However, this approach seems suboptimal as it involves searching all documents in the database.
Considering fulltext search, I added an index to the title
field and experimented with this query:
db.collection.find({ $text: { $search: value } }).count()
Unfortunately, using Samp
does not return the first document as expected.
Lastly, I am unsure how to execute a search across all five collections to consolidate all matches into a single result.