Skip to content

Commit 1c70d3a

Browse files
committed
docs: connect
1 parent e38d620 commit 1c70d3a

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

src/mongo_client.ts

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -594,20 +594,13 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
594594
}
595595

596596
/**
597-
* Connect to MongoDB using a url
597+
* An optional method to verify a typical set of preconditions before using a MongoClient.
598+
* For detailed information about the connect process see the MongoClient.connect static method documentation.
598599
*
599-
* @remarks
600-
* Calling `connect` is optional since the first operation you perform will call `connect` if it's needed.
601-
* `timeoutMS` will bound the time any operation can take before throwing a timeout error.
602-
* However, when the operation being run is automatically connecting your `MongoClient` the `timeoutMS` will not apply to the time taken to connect the MongoClient.
603-
* This means the time to setup the `MongoClient` does not count against `timeoutMS`.
604-
* If you are using `timeoutMS` we recommend connecting your client explicitly in advance of any operation to avoid this inconsistent execution time.
605-
*
606-
* @remarks
607-
* The driver will look up corresponding SRV and TXT records if the connection string starts with `mongodb+srv://`.
608-
* If those look ups throw a DNS Timeout error, the driver will retry the look up once.
600+
* @param url - The MongoDB connection string (supports `mongodb://` and `mongodb+srv://` schemes)
601+
* @param options - Optional configuration options for the client
609602
*
610-
* @see docs.mongodb.org/manual/reference/connection-string/
603+
* @see https://www.mongodb.com/docs/manual/reference/connection-string/
611604
*/
612605
async connect(): Promise<this> {
613606
if (this.connectionLock) {
@@ -868,21 +861,35 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
868861
}
869862

870863
/**
871-
* Connect to MongoDB using a url
864+
* Creates a new MongoClient instance and immediately connects it to MongoDB.
865+
* This convenience method combines `new MongoClient(url, options)` and `client.connect()` in a single step.
866+
*
867+
* Connect can be helpful to detect configuration issues early by validating:
868+
* - **DNS Resolution**: Verifies that SRV records and hostnames in the connection string resolve DNS entries
869+
* - **Network Connectivity**: Confirms that host addresses are reachable and ports are open
870+
* - **TLS Configuration**: Validates SSL/TLS certificates, CA files, and encryption settings are correct
871+
* - **Authentication**: Verifies that provided credentials are valid
872+
* - **Server Compatibility**: Ensures the MongoDB server version is supported by this driver version
873+
* - **Load Balancer Setup**: For load-balanced deployments, confirms the service is properly configured
874+
*
875+
* @returns A promise that resolves to the same MongoClient instance once connected
872876
*
873877
* @remarks
874-
* Calling `connect` is optional since the first operation you perform will call `connect` if it's needed.
875-
* `timeoutMS` will bound the time any operation can take before throwing a timeout error.
876-
* However, when the operation being run is automatically connecting your `MongoClient` the `timeoutMS` will not apply to the time taken to connect the MongoClient.
877-
* This means the time to setup the `MongoClient` does not count against `timeoutMS`.
878-
* If you are using `timeoutMS` we recommend connecting your client explicitly in advance of any operation to avoid this inconsistent execution time.
878+
* **Connection is Optional:** Calling `connect` is optional since any operation method (`find`, `insertOne`, etc.)
879+
* will automatically perform these same validation steps if the client is not already connected.
880+
* However, explicitly calling `connect` can make sense for:
881+
* - **Fail-fast Error Detection**: Non-transient connection issues (hostname unresolved, port refused connection) are discovered immediately rather than during your first operation
882+
* - **Predictable Performance**: Eliminates first connection overhead from your first database operation
879883
*
880884
* @remarks
881-
* The programmatically provided options take precedence over the URI options.
885+
* **Connection Pooling Impact:** Calling `connect` will populate the connection pool with one connection
886+
* to a server selected by the client's configured `readPreference` (defaults to primary).
882887
*
883888
* @remarks
884-
* The driver will look up corresponding SRV and TXT records if the connection string starts with `mongodb+srv://`.
885-
* If those look ups throw a DNS Timeout error, the driver will retry the look up once.
889+
* **Timeout Behavior:** When using `timeoutMS`, the connection establishment time does not count against
890+
* the timeout for subsequent operations. This means `connect` runs without a `timeoutMS` limit, while
891+
* your database operations will still respect the configured timeout. If you need predictable operation
892+
* timing with `timeoutMS`, call `connect` explicitly before performing operations.
886893
*
887894
* @see https://www.mongodb.com/docs/manual/reference/connection-string/
888895
*/

0 commit comments

Comments
 (0)