Ch4 lecture 2: $text vs. $regex

Hello dear community,

I discovered $text in ch4 lecture 2 handouts. When applying the given syntax in the companies collection:
{ $match: { "$text": { "$search": "network" } }
Output is 3249 documents.

I had the feeling that a regex syntax would be equivalent:
{ $match: { overview: /network/i } }
However, output is different: 3281 documents

Could someone explain the difference please?

Hi @Antoine_Delequeuche,

Sorry we missed your post!!

Let me check and get back to you.

Kind Regards,

The regular expression with overview only looks at the overview field. With $text and $search, you may look at all the fields of your text index. So if you have a field, for example comment that is part of the text index, documents with network in it will be found.

Let say you have the following documents:

{ "_id" : 3, "comment" : "Network" }
{ "_id" : 2, "overview" : "Network" }
{ "_id" : 1, "overview" : "network" }

and the following index:

{ "overview" : "text" , "comment" : "text" }

The regular expression will only find _id:1 and _id:2. However, with $search, all 3 documents will be found. If the text index was only on the field comment, then only _id:3 will be found.

For more information on text index, see the following.

Hey @Antoine_Delequeuche,

Closely looking into documents returned by these queries, I found that

This was not doing an exact match on “network” text. It also returned the overview field that contained for eg: “RealNetwork” and “blognetwork” texts.

This is why we see a difference of 32 fields in both queries.

Please feel free to reach out if you have any additional questions.

Kind Regards,

Hey @steevej and @Sonali_Mamgain,

Thank you for taking the time to answer me. Both your answers make sense and help get a better grasp of the $text operator.

Keep up the good work!

Kind regards,

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.