The MongoDB data source in ToolJet allows you to connect your applications directly to MongoDB databases and perform operations on collections using queries. It supports database actions such as reading, inserting, updating, and deleting documents, enabling you to work with structured and semi-structured data efficiently. This data source is ideal for building applications that require flexible schemas and fast iteration over evolving data models.
To establish a manual connection with the **MongoDB** data source, click on the **+ Add new data source** button located on the query panel or navigate to the [Data Sources](/docs/data-sources/overview) page from the ToolJet dashboard.
:::info
Please make sure the **Host/IP** of the database is accessible from your VPC if you have self-hosted ToolJet. If you are using ToolJet cloud, please **whitelist** our IP.
Use this option for MongoDB Atlas or replica set clusters.
It automatically discovers hosts using DNS, reduces configuration effort, and is the recommended approach for scalable and managed MongoDB deployments.
You can also use a **Connection String** by switching the method from the dropdown. You will be prompted to enter the details of your MongoDB connection.
ToolJet requires the following to connect to your MongoDB using Connecting String:
- **Connection String**
:::info
The connection string typically looks like this: `mongodb+srv://${username}:${password}@${cluster}/{database}`.
For example: `mongodb+srv://tooljettest:dummypassword@cluster0.urul7.mongodb.net/hrms`
**Note:** Make sure to replace username, password, cluster, and database with your actual MongoDB details. If your MongoDB instance requires additional connection options, you can usually append these options to the connection string.
ToolJet now supports SSH tunnelling for the MongoDB data source, allowing secure connections to databases hosted inside private networks. This can be used to :
- Access private databases
- Improve security
- Enable encrytped communication
- Avoid firewall rule changes
#### SSH Configuration
To securely connect to a private MongoDB database using SSH tunnelling:
1. Enable the **SSH tunnel** toggle in the MongoDB data source configuration.
2. Provide the following details:
- **SSH host** – Server hostname or IP address.
- **SSH port** – Port number (default: `22`).
- ***SSH dst host** - Destination host that the SSH tunnel forwards traffic to (usually `localhost`)
- **SSH username** – Username for the SSH server.
- **Authentication method** – Choose either:
- **Private key**
- **Password**
Once configured, ToolJet establishes a secure SSH connection. All MongoDB queries are routed through this encrypted tunnel.
1. Click on **+ Add** button of the query manager at the bottom panel of the editor and select the database added in the previous step as the data source.
2. Select the operation that you want to perform and click **Save** to save the query.
Query results can be transformed using transformations. Read our transformations documentation to see how: [link](/docs/app-builder/custom-code/transform-data)
Returns an estimation of the number of documents in the collection based on collection metadata. [Reference](https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#estimateddocumentcount)
Returns the number of documents based on the filter. [Reference](https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#countdocuments)
If your application requires the document after updating, use this instead of **Update One**. [Reference](https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#findoneandupdate)
If your application requires the document after updating, use this instead of **Replace One**. [Reference](https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#findoneandreplace)
<imgclassName="screenshot-full img-full"src="/img/datasource-reference/mongo-db/mdb-findonenreplace.png"alt="ToolJet - Mongo DB Find One and Replace"/>
If your application requires the document after deleting, use this instead of **Delete One**. [Reference](https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#findoneanddelete)
Aggregation operations are expressions you can use to produce reduced and summarized results. [Reference](https://docs.mongodb.com/drivers/node/v4.0/fundamentals/aggregation/)