|
| 1 | +.. _node-atlas-search: |
| 2 | + |
| 3 | +============ |
| 4 | +Atlas Search |
| 5 | +============ |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: full text, text analyzer, meta, pipeline, scoring, Lucene |
| 13 | + :description: Learn about how to use Atlas Search in the {+driver-long+}. |
| 14 | + |
| 15 | +.. contents:: On this page |
| 16 | + :local: |
| 17 | + :backlinks: none |
| 18 | + :depth: 2 |
| 19 | + :class: singlecol |
| 20 | + |
| 21 | +Overview |
| 22 | +-------- |
| 23 | + |
| 24 | +In this guide, you can learn how to use the {+driver-short+} to |
| 25 | +run :atlas:`Atlas Search </atlas-search/>` queries on a collection. |
| 26 | +Atlas Search enables you to perform full-text searches on collections |
| 27 | +hosted on MongoDB Atlas. Atlas Search indexes specify the behavior of the |
| 28 | +search and which fields to index. |
| 29 | + |
| 30 | +Sample Data |
| 31 | +~~~~~~~~~~~ |
| 32 | + |
| 33 | +The example in this guide uses the ``movies`` collection in the ``sample_mflix`` |
| 34 | +database from the :atlas:`Atlas sample datasets </sample-data>`. To learn how to |
| 35 | +create a free MongoDB Atlas cluster and load the sample datasets, see the |
| 36 | +:atlas:`Get Started with Atlas </getting-started>` guide. |
| 37 | + |
| 38 | +Run an Atlas Search Query |
| 39 | +------------------------- |
| 40 | + |
| 41 | +This section shows how to create an aggregation pipeline to run an |
| 42 | +Atlas Search query on a collection. In your array of pipeline stages, |
| 43 | +add the ``$search`` stage to specify the search criteria. Then, call |
| 44 | +the ``aggregate()`` method and pass your pipeline array as a parameter. |
| 45 | + |
| 46 | +.. tip:: |
| 47 | + |
| 48 | + To learn more about aggregation operations, see the :ref:`node-aggregation` |
| 49 | + guide. |
| 50 | + |
| 51 | +Before running an Atlas Search query, you must create an Atlas Search index |
| 52 | +on your collection. To learn how to programmatically create an Atlas Search |
| 53 | +index, see the :ref:`node-indexes-search` section of the Indexes guide. |
| 54 | + |
| 55 | +Atlas Search Example |
| 56 | +~~~~~~~~~~~~~~~~~~~~ |
| 57 | + |
| 58 | +This example runs an Atlas Search query by performing the |
| 59 | +following actions: |
| 60 | + |
| 61 | +- Creates a ``$search`` stage that instructs the driver |
| 62 | + to query for documents in which the ``title`` field contains |
| 63 | + the word ``"Alabama"`` |
| 64 | + |
| 65 | +- Creates a ``$project`` stage that instructs the driver to |
| 66 | + include the ``title`` field in the query results |
| 67 | + |
| 68 | +- Passes the pipeline stages to the ``aggregate()`` method and |
| 69 | + prints the results |
| 70 | + |
| 71 | +.. io-code-block:: |
| 72 | + :copyable: |
| 73 | + |
| 74 | + .. input:: /includes/atlas-search.js |
| 75 | + :start-after: begin-atlas-search |
| 76 | + :end-before: end-atlas-search |
| 77 | + :language: java |
| 78 | + :dedent: |
| 79 | + |
| 80 | + .. output:: |
| 81 | + :language: console |
| 82 | + :visible: false |
| 83 | + |
| 84 | + { |
| 85 | + _id: new ObjectId('...'), |
| 86 | + title: 'Alabama Moon' |
| 87 | + } |
| 88 | + { |
| 89 | + _id: new ObjectId('...'), |
| 90 | + title: 'Crazy in Alabama' |
| 91 | + } |
| 92 | + { |
| 93 | + _id: new ObjectId('...'), |
| 94 | + title: 'Sweet Home Alabama' |
| 95 | + } |
| 96 | + |
| 97 | +.. tip:: Node.js Driver Atlas Search Examples |
| 98 | + |
| 99 | + To view more examples that use the {+driver-short+} to perform Atlas |
| 100 | + Search queries, see :atlas:`Atlas Search Tutorials </atlas-search/tutorials/>` |
| 101 | + in the Atlas documentation. |
| 102 | + |
| 103 | +Additional Information |
| 104 | +---------------------- |
| 105 | + |
| 106 | +To learn more about Atlas Search, see :atlas:`Atlas Search </atlas-search/>` |
| 107 | +in the Atlas documentation. |
| 108 | + |
| 109 | +API Documentation |
| 110 | +~~~~~~~~~~~~~~~~~ |
| 111 | + |
| 112 | +To learn more about the ``aggregate()`` method, see the |
| 113 | +`API documentation <{+api+}/classes/Collection.html#aggregate>`__. |
0 commit comments