@@ -151,9 +151,15 @@ invocation:
151
151
Create Text Indexes
152
152
^^^^^^^^^^^^^^^^^^^
153
153
154
- To create a ``text`` index, use the following invocation of
154
+ To create a ``text`` index, use the following syntax of
155
155
:method:`~db.collection.ensureIndex()`:
156
156
157
+ .. code-block:: javascript
158
+
159
+ db.collection.ensureIndex( { <field>: "text" } )
160
+
161
+ Consider the following example:
162
+
157
163
.. code-block:: javascript
158
164
159
165
db.collection.ensureIndex( { content: "text" } )
@@ -341,9 +347,12 @@ cursor.
341
347
to only those specified.
342
348
343
349
:param number limit:
344
-
350
+
345
351
Optional. Specify the maximum number of documents to include in
346
- the response. The default limit is 100.
352
+ the response. The :dbcommand:`text` sorts the results before
353
+ applying the ``limit``.
354
+
355
+ The default limit is 100.
347
356
348
357
:param string language:
349
358
@@ -353,10 +362,11 @@ cursor.
353
362
354
363
:return:
355
364
356
- :dbcommand:`text` returns results in the form of a document.
357
- Results must fit within the :limit:`BSON Document Size`. Use the
358
- ``limit`` and the ``projection`` parameters to limit the size of
359
- the result set.
365
+ :dbcommand:`text` returns results, in descending order by score,
366
+ in the form of a document. Results must fit within the
367
+ :limit:`BSON Document Size`. Use the ``limit`` and the
368
+ ``projection`` parameters to limit the size of the result set.
369
+
360
370
361
371
The implicit connector between the terms of a multi-term search is a
362
372
disjunction (``OR``). Search for ``"first second"`` searches
@@ -387,43 +397,43 @@ cursor.
387
397
388
398
db.collection.ensureIndex( { content: "text" } )
389
399
390
- #. Search for a single word ``search ``:
400
+ #. Search for a single word ``coffee ``:
391
401
392
402
.. code-block:: javascript
393
403
394
- db.collection.runCommand( "text", { search: "search " } )
404
+ db.collection.runCommand( "text", { search: "coffee " } )
395
405
396
- This query returns documents that contain the word ``search ``,
406
+ This query returns documents that contain the word ``coffee ``,
397
407
case-insensitive, in the ``content`` field.
398
408
399
- #. Search for multiple words, ``create `` or ``search `` or ``fields ``:
409
+ #. Search for multiple words, ``bake `` or ``coffee `` or ``cake ``:
400
410
401
411
.. code-block:: javascript
402
412
403
- db.collection.runCommand( "text", { search: "create search fields " } )
413
+ db.collection.runCommand( "text", { search: "bake coffee cake " } )
404
414
405
- This query returns documents that contain the either ``create ``
406
- **or** ``search `` **or** ``field `` in the ``content`` field.
415
+ This query returns documents that contain the either ``bake ``
416
+ **or** ``coffee `` **or** ``cake `` in the ``content`` field.
407
417
408
- #. Search for the exact phrase ``create search fields ``:
418
+ #. Search for the exact phrase ``bake coffee cake ``:
409
419
410
420
.. code-block:: javascript
411
421
412
- db.collection.runCommand( "text", { search: "\"create search fields \"" } )
422
+ db.collection.runCommand( "text", { search: "\"bake coffee cake \"" } )
413
423
414
424
This query returns documents that contain the exact phrase
415
- ``create search fields``.
425
+ ``bake coffee cake``.
426
+
427
+ #. Search for documents that contain the words ``bake`` or ``coffee``,
428
+ but **not** ``cake``:
416
429
417
- #. Search for documents that contain the words ``create`` or ``search``,
418
- but **not** ``fields``:
419
-
420
430
.. code-block:: javascript
421
-
422
- db.collection.runCommand( "text", { search: "create search -fields " } )
423
-
431
+
432
+ db.collection.runCommand( "text", { search: "bake coffee -cake " } )
433
+
424
434
Use the ``-`` as a prefix to terms to specify negation in the
425
435
search string. The query returns documents that contain the
426
- either ``create `` **or** ``search ``, but **not** ``field ``, all
436
+ either ``bake `` **or** ``coffee ``, but **not** ``cake ``, all
427
437
case-insensitive, in the ``content`` field. Prefixing a word
428
438
with a hyphen (``-``) negates a word:
429
439
@@ -436,25 +446,24 @@ cursor.
436
446
negation. The :dbcommand:`text` command treats the hyphen as a
437
447
delimiter.
438
448
439
- #. Search for a single word ``search `` with an additional ``filter`` on
449
+ #. Search for a single word ``coffee `` with an additional ``filter`` on
440
450
the ``about`` field, but **limit** the results to 2 documents with the
441
451
highest score and return only the ``comments`` field in the matching
442
452
documents:
443
453
444
454
.. code-block:: javascript
445
455
446
456
db.collection.runCommand( "text", {
447
- search: "insensitive ",
448
- filter: { about: /something / },
457
+ search: "coffee ",
458
+ filter: { about: /desserts / },
449
459
limit: 2,
450
460
projection: { comments: 1, _id: 0 }
451
461
}
452
462
)
453
463
454
464
- The ``filter`` :ref:`query document <mongodb-query-document>`
455
- uses a :operator:`regular expression <$regex>`. See the
456
- :doc:`query operators </reference/operator>` page for available query
457
- operators.
465
+ may use any of the available :doc:`query operators
466
+ </reference/operator>`.
458
467
459
468
- Because the ``_id`` field is implicitly included, in order to
460
469
return **only** the ``comments`` field, you must explicitly
0 commit comments