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
@@ -594,20 +594,13 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
594
594
}
595
595
596
596
/**
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.
598
599
*
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
@@ -868,21 +861,35 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
868
861
}
869
862
870
863
/**
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
872
876
*
873
877
* @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
879
883
*
880
884
* @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).
882
887
*
883
888
* @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.
0 commit comments