0

I want to do a wildcard search on the Whole collection, irrespective of the Columns or rows.

ID Name Subject Expert
1 John Hill Science Maths
2 Matt Jane English Spanish
3 Tom Hill Maths Chemistry

1.

   mongoose.connection.db.collection('CollectionName').find({"name":/Hil/}).toArray(function (err,data){});

I get both ID 1 and 3

I tried using the $text $search function after indexing the collection

2.

   mongoose.connection.db.collection('CollectionName')find({$text:{$search:"Maths"}}).toArray(function (err,data){});

I get both ID 1 and 3

3.

   mongoose.connection.db.collection('CollectionName')find({$text:{$search:/Mat/}}).toArray(function (err,data){});

The above doesn't work.

I want to perform a wildcard search on both the column and row something like .find("wildcard":"wildcard")

Is it possible to achieve the above ?

Runesh
  • 1
  • 1

1 Answers1

0

Why not using $or with regex pattern?

const match = /Mat/;

const query = collection.find({
    $or: [{ name: match }, { subject: match }, { expert: match }],
});

Almaju
  • 1,283
  • 12
  • 31
  • 1. I might not know the column names, as it is generated dynamically 2. If I have 50+ column then the above would be a tedious process – Runesh Mar 24 '22 at 02:10
  • Sounds like it is not possible with plain mongodb. You can read this: https://stackoverflow.com/questions/6790819/searching-for-value-of-any-field-in-mongodb-without-explicitly-naming-it – Almaju Mar 24 '22 at 10:03