Disconnect
Disconnects from a MongoDB database server and releases the client connection resources.
Common Properties
- Name - The custom name of the node.
- Color - The custom color of the node.
- Delay Before (sec) - Waits in seconds before executing the node.
- Delay After (sec) - Waits in seconds after executing node.
- Continue On Error - Automation will continue regardless of any error. The default value is false.
info
If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.
Inputs
- MongoDB Client Id - The client ID returned from the Connect node that identifies the connection to close.
How It Works
The Disconnect node properly closes a MongoDB connection and releases associated resources. When executed, the node:
- Validates the provided client ID
- Retrieves the MongoDB client connection from the internal registry
- Calls the disconnect method on the MongoDB client
- Removes the client from the internal registry
- Releases all associated resources
Requirements
- A valid client ID from a previous Connect node execution
- The connection must still be active (not already disconnected)
Error Handling
The node will return specific errors in the following cases:
- ErrInvalidArg - The provided client ID is invalid or not found in the registry
- Connection errors if the disconnection process fails
Usage Notes
- Always use Disconnect to properly close MongoDB connections
- The client ID becomes invalid after disconnection
- Attempting to use a disconnected client ID in other nodes will result in errors
- Resources are automatically cleaned up when a flow stops, but explicit disconnection is recommended
- Disconnect should typically be the last MongoDB operation in your flow
Best Practices
- Always pair Connect with Disconnect nodes in your flows
- Place the Disconnect node at the end of your MongoDB operations
- Use error handling to ensure Disconnect runs even if previous operations fail
- Consider using a Try-Catch-Finally pattern with Disconnect in the Finally block
- Don't reuse client IDs after disconnection
Example Usage
Scenario: Proper connection lifecycle management
- Start flow with Connect node → stores Client Id
- Perform multiple database operations using the Client Id
- End flow with Disconnect node to clean up
Scenario: Error-safe disconnection
Try Block:
- Connect node → Client Id
- Database operations...
Catch Block:
- Log error
Finally Block:
- Disconnect using Client Id (ensures cleanup even on error)
Common Errors
Invalid Client ID Error:
- Cause: The client ID doesn't exist or was already disconnected
- Solution: Verify the client ID is correctly passed from the Connect node and hasn't been used in a previous Disconnect
Connection Already Closed:
- Cause: Attempting to disconnect an already closed connection
- Solution: Ensure Disconnect is only called once per connection