File: connect.txt

package info (click to toggle)
node-mongodb 3.6.4%2B~cs11.13.19-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 76,604 kB
  • sloc: javascript: 138,083; python: 429; sh: 52; makefile: 37
file content (99 lines) | stat: -rw-r--r-- 3,619 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
==================
Connect to MongoDB
==================

Use the ``client.connect`` method to connect to a running MongoDB deployment.

Connect to a Single MongoDB Instance
------------------------------------

To connect to a single MongoDB instance, specify the URI of the MongoDB
instance to connect to.

In the following example, the
:manual:`URI connection string </reference/connection-string/>`
specifies connecting to a MongoDB instance that is running on
``localhost`` using port ``27017``. The ``myproject`` indicates the database
to use.

.. literalinclude:: /includes/basic-connection.js
   :language: js

For more information on the URI connection string, see
:manual:`URI connection string </reference/connection-string/>` .

Connect to a Replica Set
------------------------

To connect to a :manual:`replica set </core/replication-introduction/>` ,
include a seedlist of replica set members and the name of the replica set in the
:manual:`URI connection string </reference/connection-string/>` .

In the following example, the connection string specifies two of the replica set members running on ``localhost:27017`` and ``localhost:27018`` and the name of the replica set (\ ``foo``\ ).

.. literalinclude:: /includes/connect-to-replicaset.js
   :language: js

For more information on the URI connection string, see
:manual:`URI connection string </reference/connection-string/>` .

Connect to Sharded Cluster
--------------------------

To connect to a :manual:`sharded cluster </core/sharded-cluster-components/>` , specify the ``mongos`` instance or instances in the :manual:`URI connection string </reference/connection-string/>` .

In the following example, the connection string specifies the ``mongos`` instances running on ``localhost:50000`` and ``localhost:50001``.

.. literalinclude:: /includes/connect-to-sharded-cluster.js
   :language: js

For more information on the URI connection string, see
:manual:`URI connection string </reference/connection-string/>` .

Connection Options
------------------

You can specify various connection settings in the :manual:`URI connection string </reference/connection-string/>` .

For example, you can specify TLS/SSL and authentication setting.

.. code-block:: js

   const { MongoClient } = require('mongodb');
   const fs = require('fs');

   // Connection URL
   const url = 'mongodb://dave:password@localhost:27017?authMechanism=DEFAULT&authSource=db&tls=true';

   // Create a client, passing in additional options
   const client = new MongoClient(url,  {
     tlsCAFile: path.resolve(__dirname + '/certs/ca.pem'),
     tlsCertificateKeyFile: `${__dirname}/certs/client.pem`
   });

   // Function to connect to the server
   async function run() {
     try {
       // Connect the client to the server
       await client.connect();
       console.log('Connected successfully to server');
     } finally {
       // Ensures that the client will close when you finish/error
       await client.close();
     }
   }

   // Runs your code
   run();


For more information on connecting with authentication and TSL/SSL, see:


* :doc:`Authentication </tutorials/connect/authentication>` : detailed documentation of the various ways to specify authentication credentials
* :doc:`TLS/SSL </tutorials/connect/tls>` : Detailed documentation of the various ways to specify the properties of an TLS/SSL connection

For more information on the connection options:

* :manual:`URI connection string </reference/connection-string/>` : MongoDB connection string URI.
* :doc:`/connection-settings` : Reference on the driver-specific connection settings.