You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/mongo_client.ts
+28-21Lines changed: 28 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -569,20 +569,13 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
569
569
}
570
570
571
571
/**
572
-
* Connect to MongoDB using a url
572
+
* An optional method to verify a handful of assumptions that are generally useful at application boot-time before using a MongoClient.
573
+
* For detailed information about the connect process see the MongoClient.connect static method documentation.
573
574
*
574
-
* @remarks
575
-
* Calling `connect` is optional since the first operation you perform will call `connect` if it's needed.
576
-
* `timeoutMS` will bound the time any operation can take before throwing a timeout error.
577
-
* However, when the operation being run is automatically connecting your `MongoClient` the `timeoutMS` will not apply to the time taken to connect the MongoClient.
578
-
* This means the time to setup the `MongoClient` does not count against `timeoutMS`.
579
-
* If you are using `timeoutMS` we recommend connecting your client explicitly in advance of any operation to avoid this inconsistent execution time.
580
-
*
581
-
* @remarks
582
-
* The driver will look up corresponding SRV and TXT records if the connection string starts with `mongodb+srv://`.
583
-
* If those look ups throw a DNS Timeout error, the driver will retry the look up once.
575
+
* @param url - The MongoDB connection string (supports `mongodb://` and `mongodb+srv://` schemes)
576
+
* @param options - Optional configuration options for the client
@@ -843,21 +836,35 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
843
836
}
844
837
845
838
/**
846
-
* Connect to MongoDB using a url
839
+
* Creates a new MongoClient instance and immediately connects it to MongoDB.
840
+
* This convenience method combines `new MongoClient(url, options)` and `client.connect()` in a single step.
841
+
*
842
+
* Connect can be helpful to detect configuration issues early by validating:
843
+
* - **DNS Resolution**: Verifies that SRV records and hostnames in the connection string resolve DNS entries
844
+
* - **Network Connectivity**: Confirms that host addresses are reachable and ports are open
845
+
* - **TLS Configuration**: Validates SSL/TLS certificates, CA files, and encryption settings are correct
846
+
* - **Authentication**: Verifies that provided credentials are valid
847
+
* - **Server Compatibility**: Ensures the MongoDB server version is supported by this driver version
848
+
* - **Load Balancer Setup**: For load-balanced deployments, confirms the service is properly configured
849
+
*
850
+
* @returns A promise that resolves to the same MongoClient instance once connected
847
851
*
848
852
* @remarks
849
-
* Calling `connect` is optional since the first operation you perform will call `connect` if it's needed.
850
-
* `timeoutMS` will bound the time any operation can take before throwing a timeout error.
851
-
* However, when the operation being run is automatically connecting your `MongoClient` the `timeoutMS` will not apply to the time taken to connect the MongoClient.
852
-
* This means the time to setup the `MongoClient` does not count against `timeoutMS`.
853
-
* If you are using `timeoutMS` we recommend connecting your client explicitly in advance of any operation to avoid this inconsistent execution time.
853
+
* **Connection is Optional:** Calling `connect` is optional since any operation method (`find`, `insertOne`, etc.)
854
+
* will automatically perform these same validation steps if the client is not already connected.
855
+
* However, explicitly calling `connect` can make sense for:
856
+
* - **Fail-fast Error Detection**: Non-transient connection issues (hostname unresolved, port refused connection) are discovered immediately rather than during your first operation
857
+
* - **Predictable Performance**: Eliminates first connection overhead from your first database operation
854
858
*
855
859
* @remarks
856
-
* The programmatically provided options take precedence over the URI options.
860
+
* **Connection Pooling Impact:** Calling `connect` will populate the connection pool with one connection
861
+
* to a server selected by the client's configured `readPreference` (defaults to primary).
857
862
*
858
863
* @remarks
859
-
* The driver will look up corresponding SRV and TXT records if the connection string starts with `mongodb+srv://`.
860
-
* If those look ups throw a DNS Timeout error, the driver will retry the look up once.
864
+
* **Timeout Behavior:** When using `timeoutMS`, the connection establishment time does not count against
865
+
* the timeout for subsequent operations. This means `connect` runs without a `timeoutMS` limit, while
866
+
* your database operations will still respect the configured timeout. If you need predictable operation
867
+
* timing with `timeoutMS`, call `connect` explicitly before performing operations.
0 commit comments