Skip to content

Commit 9b60866

Browse files
norareidylindseymoore
authored andcommitted
DOCSP-48384: Atlas search (mongodb#1041)
* DOCSP-48384: Atlas search * edits * tech feedback
1 parent 1f9e8f5 commit 9b60866

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

source/atlas-search.txt

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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>`__.

source/includes/atlas-search.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { MongoClient } = require("mongodb");
2+
3+
// Replace the placeholder with your connection string.
4+
const uri = "<connection string>";
5+
const client = new MongoClient(uri);
6+
7+
async function run() {
8+
try {
9+
const database = client.db("sample_mflix");
10+
const collection = database.collection("movies");
11+
12+
// Queries for documents that have a "title" value containing the word "Alabama"
13+
// begin-atlas-search
14+
const pipeline = [
15+
{
16+
$search: {
17+
index: "default", // Replace with your search index name
18+
text: {
19+
query: "Alabama",
20+
path: "title"
21+
}
22+
}
23+
},
24+
{
25+
$project: {
26+
title: 1
27+
}
28+
}
29+
];
30+
31+
const cursor = collection.aggregate(pipeline);
32+
for await (const document of cursor) {
33+
console.log(document);
34+
}
35+
// end-atlas-search
36+
} finally {
37+
await client.close();
38+
}
39+
}
40+
41+
run().catch(console.dir);

source/index.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ MongoDB Node Driver
2323
Data Formats </data-formats>
2424
Indexes </indexes>
2525
Run a Database Command </run-command>
26+
Atlas Search </atlas-search>
2627
Monitoring and Logging </monitoring-and-logging>
2728
Security </security>
2829
Reference </reference>
@@ -85,6 +86,11 @@ Run a Database Command
8586

8687
Learn how to run a database command in the :ref:`<node-run-command>` section.
8788

89+
Atlas Search
90+
------------
91+
92+
Learn how to run Atlas Search queries in the :ref:`<node-atlas-search>` section.
93+
8894
Monitoring and Logging
8995
----------------------
9096

0 commit comments

Comments
 (0)