Search mongo db using mongoose regular expression and text

Can someone explain the difference between:

db.collection.find({ $text: { $search: "dog cat" } })

and

Product.find({ "drug": { "$regex": "cols", "$options": "i" } })

When should we go, for what?

+4
source share
2 answers

$ regex

Provides regular expression capabilities for pattern matching strings in queries. MongoDB uses Perl-compatible regular expressions (ie "PCRE") version 8.36 with UTF-8 support.

$ text

Performs a text search on the contents of fields indexed using a text index .


For data

{ "_id" : 100, "sku" : "abc123", "description" : "Single line description." }
{ "_id" : 101, "sku" : "abc789", "description" : "First line\nSecond line" }
{ "_id" : 102, "sku" : "xyz456", "description" : "Many spaces before     line" }
{ "_id" : 103, "sku" : "xyz789", "description" : "Multiple\nline description" }

In the search field sku, which is a single-line value, you can use$regex

db.products.find( { sku: { $regex: /^ABC/i } } )

description, , $text, , description.

db.products.find( { $text: { $search: "coffee" } } )

?

  • regular expressions , - , "action -superhero" .
  • , .

.

+2

, , ($ text) . , .

  • Regex , ^.

  • Regex . . * .

  • Regex .

$

mongodb . MongoDB . , . , , bobcat .

, RDBMS like, "$ text" ( , MongoDB, ).

+2

All Articles