@@ -4,48 +4,45 @@ Search Text
44
55.. default-domain:: mongodb
66
7- Use the :manual:`$text
8- </reference/operator/query/text/>`
9- operator to perform text searches on fields which have a
10- :manual:`text index </core/index-text/>`.
11-
127Overview
138--------
149
15- Text searches let you search a collection of string-valued fields for
16- words or phrases, instead of matching substrings like
17- :mdn:`regular expressions
18- <Web/JavaScript/Guide/Regular_Expressions>`,
19- also known as RegEx. While regex is very powerful, and MongoDB even
20- offers a ``$regex`` operator, text searches work particularly well when
21- applied to unstructured text, like transcripts, essays, or even web
22- pages. Because MongoDB's text indexes are aware of concepts like
23- plurality, case sensitivity, stop words, whitespace, and punctuation in
24- multiple languages, all you have to do is query with a search term to
25- find documents that contain your term.
26-
10+ Text search, using the ``$text`` query operator, lets you search string
11+ type fields in your collection for words or phrases. This operator
12+ performs a logical ``OR`` on each term separated by a space in the search
13+ string. You can also specify additional options to the operator to
14+ handle case sensitivity, word stemming (e.g. plural forms, tense) and stop
15+ words for a supported language. This is particularly useful for
16+ unstructured text such as transcripts, essays, or web pages.
2717
28- .. admonition::
18+ The ``$text`` query operator requires that you specify the search field in
19+ a **text index** on your collection. See the examples below for sample
20+ code for creating a text index and using the ``$text`` query operator.
2921
30- You can only create *one* text index per collection, so every text
31- search queries all fields used by the index for term matches.
3222
3323Examples
3424--------
3525
36- The following examples use the ``movies`` collection in the
37- ``sample_mflix`` sample database and assume that you have created a text
38- index on the ``title`` field.
26+ The following examples use the ``movies`` collection in the ``sample_mflix``
27+ database. In order to enable text searches on the ``title`` field, create
28+ the **text index** using the following command:
29+
30+ .. code-block:: javascript
3931
40- .. note::
32+ db.movies.createIndex({ title: "text" });
33+
34+ We use this text index for the examples, but you can create a compound
35+ text index that broadens your text queries to multiple fields as follows:
36+
37+ .. code-block:: javascript
4138
42- Text searches require a text index. To create a text index on the
43- ``title`` field of the ``movies`` collection of the ``sample_mflix``
44- database, use the following command on the sample_mflix database:
39+ db.movies.createIndex({ title: "text", fullplot: "text" });
4540
46- .. code-block:: javascript
41+ You can only create *one* text index per collection. Every text search
42+ queries all the fields specified in that index for matches.
4743
48- db.movies.createIndex({title: "text"})
44+ See the MongoDB server manual for more information on creating
45+ :manual:`text indexes </core/index-text/>`.
4946
5047Query for Words
5148~~~~~~~~~~~~~~~
@@ -80,10 +77,10 @@ This operation returns the following documents:
8077 { title: 'Star Trek II: The Wrath of Khan' }
8178
8279Success! The query found every document in the ``movies`` collection
83- with a title including the word "trek". Unfortunately, it looks like the
84- search included one unintended item: "Trek Nation," which is a movie
85- about Star Trek and not a Star Trek movie. To solve this, we can query
86- with a more specific **phrase**.
80+ with a title including the word "trek". Unfortunately, the search included
81+ one unintended item: "Trek Nation," which is a movie about Star Trek and not
82+ part of the Star Trek movie series . To solve this, we can query with a more
83+ specific **phrase**.
8784
8885Query By Phrase
8986~~~~~~~~~~~~~~~
@@ -119,18 +116,16 @@ matches the following documents:
119116These results include all movies in the database that contain the phrase
120117"star trek", which in this case results in only fictional Star Trek
121118movies. Unfortunately, though, this query returned "Star Trek Into
122- Darkness", a movie that nobody liked much. To solve this, we can omit
123- that document with a **negation**.
119+ Darkness", a movie that was not part of the original series of movies. To
120+ resolve this issue, we can omit that document with a **negation**.
124121
125122Query with Negations
126123~~~~~~~~~~~~~~~~~~~~
127124
128- Because you probably don't want to be associated with such an unpopular
129- movie, you can choose to exclude it from the results set using a negated
130- term. To use a negated term, place a negative sign (``-``) in front of
131- the term you'd like to omit from the result set. Any documents that
132- contain this term will not show up in your results. Since this query
133- includes two terms, separate the terms with spaces.
125+ To use a negated term, place a negative sign (``-``) in front of the term
126+ you would like to omit from the result set. The query operation omits any
127+ documents that contain this term from the search result. Since this query
128+ includes two distinct terms, separate them with a space.
134129
135130.. literalinclude:: /code-snippets/crud/startrek.js
136131 :language: javascript
0 commit comments