Skip to content

Commit 3e491d9

Browse files
authored
Merge branch 'comp-cov' into DOCSP-48385-avs-page
2 parents 5738a01 + 981015b commit 3e491d9

File tree

8 files changed

+592
-98
lines changed

8 files changed

+592
-98
lines changed

snooty.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ mdb-server = "MongoDB Server"
3232
min-node-version = "v16.20.1"
3333
stable-api = "Stable API"
3434
vector-search = "Atlas Vector Search"
35+
language = "JavaScript"

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/connect/mongoclient.txt

Lines changed: 60 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -18,54 +18,71 @@ Create a MongoClient
1818
:depth: 2
1919
:class: singlecol
2020

21-
This guide shows you how to connect to a
22-
`MongoDB Atlas deployment <https://www.mongodb.com/docs/atlas>`__,
23-
a MongoDB instance, or a replica set using the Node.js driver.
21+
Overview
22+
--------
23+
24+
To connect to a MongoDB deployment, you need two things:
25+
26+
- **Connection URI**, also known as a *connection string*, which tells the
27+
{+driver-short+} which MongoDB deployment to connect to.
28+
- **MongoClient** object, which creates the connection to and performs
29+
operations on the MongoDB deployment.
30+
31+
You can also use ``MongoClientOptions`` to customize the way the {+driver-short+} behaves
32+
while connected to MongoDB.
33+
34+
This guide shows you how to create a connection string and use a ``MongoClient`` object
35+
to connect to MongoDB.
2436

2537
.. _node-connection-uri:
2638

2739
Connection URI
2840
--------------
2941

30-
The **connection URI** is the set of instructions that the driver uses to connect to a
31-
MongoDB deployment. It tells the driver how to connect to MongoDB and how to behave
32-
while connected. The following example shows each part of the connection URI:
42+
A standard connection string includes the following components:
3343

34-
.. figure:: /includes/figures/dns_seedlist_connection_string_parts.png
35-
:alt: Each part of the connection string
44+
.. list-table::
45+
:widths: 20 80
46+
:header-rows: 1
3647

37-
In this example, we connect to an Atlas MongoDB deployment that has a
38-
DNS SRV record. For more details, see the
39-
:manual:`DNS Seed List Connection Format
40-
</reference/connection-string/#dns-seed-list-connection-format>`
41-
documentation. This format offers flexibility in deployment and the
42-
ability to change the servers in rotation without reconfiguring clients.
48+
* - Component
49+
- Description
4350

44-
.. note::
51+
* - ``mongodb://``
52+
53+
- Required. A prefix that identifies this as a string in the
54+
standard connection format.
4555

46-
To learn how to retrieve your connection string in Atlas, see the
47-
:atlas:`Atlas driver connection guide </driver-connection>`.
56+
* - ``username:password``
4857

49-
If you are connecting to an instance or replica set that does not have a
50-
DNS SRV address, you must use ``mongodb`` for the protocol, which specifies
51-
the :manual:`Standard Connection String Format
52-
</reference/connection-string/#std-label-connections-standard-connection-string-format>`.
58+
- Optional. Authentication credentials. If you include these, the client
59+
authenticates the user against the database specified in ``authSource``.
60+
For more information about the ``authSource`` connection option,
61+
see :ref:`node-troubleshooting-connection-admin` in the Connection Troubleshooting guide.
5362

54-
After the protocol, the next part of the connection string contains credentials
55-
if you are using password-based authentication. Replace the value of ``user``
56-
with your username and ``pass`` with your password. If you are using an
57-
authentication mechanism that does not require a username and password, omit
58-
this part of the connection URI.
63+
* - ``host[:port]``
5964

60-
The next part of the connection string specifies the hostname or IP address of
61-
your MongoDB instance, followed by the port number. In the example above, we use
62-
``sample.host`` as the hostname and ``27017`` as the port. Replace these values
63-
to point to your MongoDB instance.
65+
- Required. The host and optional port number where MongoDB is running. If you don't
66+
include the port number, the driver uses the default port, ``27017``.
6467

65-
The last part of the connection string contains connection and authentication
66-
options as parameters. In the example above, we set two connection options:
67-
``maxPoolSize=20`` and ``w=majority``. For more information on connection
68-
options, skip to the :ref:`node-connection-options` section.
68+
* - ``/defaultauthdb``
69+
70+
- Optional. The authentication database to use if the
71+
connection string includes ``username:password@``
72+
authentication credentials but not the ``authSource`` option.
73+
When you call ``client.db()`` with no argument, this is the database that is used. If you don't include
74+
this component, the client authenticates the user against the ``admin`` database.
75+
76+
* - ``?<options>``
77+
78+
- Optional. A query string that specifies connection-specific
79+
options as ``<name>=<value>`` pairs. See
80+
:ref:`node-connection-options` for a full description of
81+
these options.
82+
83+
For more information about creating a connection string, see
84+
:manual:`Connection Strings </reference/connection-string>` in the
85+
MongoDB Server documentation.
6986

7087
.. _connect-sample-node-driver:
7188

@@ -81,8 +98,8 @@ URI and a ``MongoClientOptions`` object.
8198
As each ``MongoClient`` represents a pool of connections to the
8299
database, most applications only require a single instance of a
83100
``MongoClient``, even across multiple requests. To learn more about
84-
how connection pools work in the driver, see the :ref:`FAQ page
85-
<node-faq-connection-pool>`.
101+
how connection pools work in the driver, see the :ref:`Connection Pools page
102+
<node-connection-pools>`.
86103

87104
Use the ``serverApi`` option in your ``MongoClientOptions`` object to
88105
enable the {+stable-api+} feature, which forces the server to run operations
@@ -102,63 +119,13 @@ verify that the connection is successful:
102119
Call the ``MongoClient.connect()`` method explicitly if you want to verify that the
103120
connection is successful.
104121

105-
To learn more about the Stable
106-
API feature, see the :ref:`{+stable-api+} page <nodejs-stable-api>`.
107-
108-
.. _node-other-ways-to-connect:
109-
110-
Other Ways to Connect to MongoDB
111-
--------------------------------
112-
113-
If you are connecting to a single {+mdb-server+} instance or replica set
114-
that is not hosted on Atlas, see the following sections to find out how to
115-
connect.
116-
117-
Connect to a {+mdb-server+} on Your Local Machine
118-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
119-
120-
.. include:: /includes/localhost-connection.rst
121-
122-
To test whether you can connect to your server, replace the connection
123-
string in the :ref:`Connect to MongoDB <connect-sample-node-driver>` code
124-
example and run it.
125-
126-
Connect to a Replica Set
127-
^^^^^^^^^^^^^^^^^^^^^^^^
128-
129-
A MongoDB replica set deployment is a group of connected instances that
130-
store the same set of data. This configuration of instances provides data
131-
redundancy and high data availability.
132-
133-
To connect to a replica set deployment, specify the hostname and port numbers
134-
of each instance, separated by a comma, and the replica set name as the value
135-
of the ``replicaSet`` parameter in the connection string.
136-
137-
.. code-block:: none
138-
139-
mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myRs
140-
141-
When making a connection, the driver takes the following actions by default:
142-
143-
- Discovers all replica set members when given the address of any one member.
144-
- Dispatches operations to the appropriate member, such as write against the **primary**.
145-
146-
.. tip:: Specify all hosts
147-
148-
To ensure connectivity if one host is unavailable, provide the full
149-
list of hosts when connecting to a replica set.
122+
To learn more about the {+stable-api+} feature, see the :ref:`{+stable-api+} page <nodejs-stable-api>`.
150123

151-
Direct Connection
152-
^^^^^^^^^^^^^^^^^
124+
API Documentation
125+
-----------------
153126

154-
To force your operations to run on the host specified in your connection
155-
URI, you can specify the ``directConnection`` connection option. If you
156-
specify this option, you must use the standard connection URI format. The
157-
driver does not accept the DNS seedlist connection format (SRV) when you
158-
specify this option.
127+
For more information about creating a ``MongoClient`` object with the
128+
{+driver-short+}, see the following API documentation:
159129

160-
When you specify ``directConnection`` and connect to a secondary member of the
161-
replica set, your write operations fail because the client isn't
162-
connected to the primary member. To perform read operations, you must
163-
enable secondary reads. See the :manual:`read preference options </reference/connection-string/#read-preference-options>`
164-
for more information.
130+
- `MongoClient <{+api+}/classes/MongoClient.html>`__
131+
- `MongoClientOptions <{+api+}/interfaces/MongoClientOptions.html>`__

0 commit comments

Comments
 (0)