ToolJet can connect to MongoDB to read and write data.
<divstyle={{paddingTop:'24px'}}>
## Manual Connection
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.
:::
ToolJet requires the following to connect to your MongoDB.
- **Host**
- **Port**
- **Username**
- **Password**
**Note:** It is recommended to create a new MongoDB user so that you can control the access levels of ToolJet.
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.
</div>
<divstyle={{paddingTop:'24px'}}>
## Querying MongoDB
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)
- **[Find One and Replace](#find-one-and-replace)**
- **[Find One and Delete](#find-one-and-delete)**
- **[Aggregate](#aggregate)**
- **[Delete One](#delete-one)**
- **[Delete Many](#delete-many)**
- **[Bulk Operations](#bulk-operations)**
### List Collections
Returns list of collections
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/listCollection.png"alt="ToolJet - Mongo DB List Collection"style={{marginBottom:'15px'}}/>
### Find One
Return a document which satisfy the given filter and options. [Reference](https://docs.mongodb.com/drivers/node/v4.0/usage-examples/findOne)
#### Required Parameters:
- **Collection**
#### Optional Parameters:
- **Filter**
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/findOne.png"alt="ToolJet - Mongo DB Find One"style={{marginBottom:'15px'}}/>
### Find Many
Return list of documents which satisfy the given filter and options. [Reference](https://docs.mongodb.com/drivers/node/v4.0/usage-examples/find/)
#### Required Parameters:
- **Collection**
#### Optional Parameters:
- **Filter**
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/findMany.png"alt="ToolJet - Mongo DB Find Many"style={{marginBottom:'15px'}}/>
### Total Count
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)
#### Required Parameters:
- **Collection**
#### Optional Parameters:
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/totalCount.png"alt="ToolJet - Mongo DB Total Count"style={{marginBottom:'15px'}}/>
### Count
Returns the number of documents based on the filter. [Reference](https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#countdocuments)
#### Required Parameters:
- **Collection**
#### Optional Parameters:
- **Filter**
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/count.png"alt="ToolJet - Mongo DB Count"style={{marginBottom:'15px'}}/>
### Distinct
Retrieve a list of distinct values for a field based on the filter. [Reference](https://docs.mongodb.com/drivers/node/v4.0/usage-examples/distinct/)
#### Required Parameters:
- **Collection**
- **Field**
#### Optional Parameters:
- **Filter**
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/distinct.png"alt="ToolJet - Mongo DB Find One"style={{marginBottom:'15px'}}/>
### Insert One
Insert a document. [Reference](https://docs.mongodb.com/drivers/node/v4.0/usage-examples/insertOne/)
#### Required Parameters:
- **Collection**
- **Document**
#### Optional Parameters:
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/insertOne.png"alt="ToolJet - Mongo DB Insert One"style={{marginBottom:'15px'}}/>
#### Example:
```json
{
"name": "John Doe",
"age": 30
}
```
### Insert Many
Insert list of documents. [Reference](https://docs.mongodb.com/drivers/node/v4.0/usage-examples/insertMany/)
#### Required Parameters:
- **Collection**
- **Document**
#### Optional Parameters:
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/insertMany.png"alt="ToolJet - Mongo DB Insert Many"style={{marginBottom:'15px'}}/>
#### Example
```json
[
{
"name": "Product1",
"price": 100
},
{
"name": "Product2",
"price": 150
}
]
```
### Update One
Update a document based on the filter. [Reference](https://docs.mongodb.com/drivers/node/v4.0/usage-examples/updateOne/)
#### Required Parameters:
- **Collection**
- **Filter**
- **Update**
#### Optional Parameters:
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/updateOne.png"alt="ToolJet - Mongo DB Update One"style={{marginBottom:'15px'}}/>
#### Example
##### Filter
```json
{
"name": "John Doe"
}
```
##### Update
```json
{
"$set": {
"age": 31
}
}
```
### Update Many
Update many documents based on the filter. [Reference](https://docs.mongodb.com/drivers/node/v4.0/usage-examples/updateMany/)
#### Required Parameters:
- **Collection**
- **Filter**
- **Update**
#### Optional Parameters:
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/updateMany.png"alt="ToolJet - Mongo DB Update Many"style={{marginBottom:'15px'}}/>
#### Example
##### Filter
```json
{
"status": "pending"
}
```
##### Update
```json
{
"$set": {
"status": "completed"
}
}
```
### Replace One
Replace a document based on filter. [Reference](https://docs.mongodb.com/drivers/node/v4.0/usage-examples/replaceOne/)
#### Required Parameters:
- **Collection**
- **Filter**
- **Replacement**
#### Optional Parameters:
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/replaceOne.png"alt="ToolJet - Mongo DB Find One"style={{marginBottom:'15px'}}/>
#### Example
##### Filter
```json
{
"product_id": 123
}
```
##### Replacement
```json
{
"product_id": 123,
"name": "New Product",
"price": 200
}
```
### Find One and Update
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)
#### Required Parameters:
- **Collection**
- **Filter**
- **Update**
#### Optional Parameters:
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/findOneUpdate.png"alt="ToolJet - Mongo DB Find One and Update"style={{marginBottom:'15px'}}/>
#### Example
##### Filter
```json
{
"employee_id": 456
}
```
##### Update
```json
{
"$inc": {
"salary": 5000
}
}
```
### Find One and Replace
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)
#### Required Parameters:
- **Collection**
- **Filter**
- **Replacement**
#### Optional Parameters:
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/findOneReplace.png"alt="ToolJet - Mongo DB Find One and Replace"style={{marginBottom:'15px'}}/>
#### Example
##### Filter
```json
{
"product_id": 789
}
```
##### Replacement
```json
{
"product_id": 789,
"name": "Updated Product",
"price": 300
}
```
### Find One and Delete
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)
#### Required Parameters:
- **Collection**
- **Filter**
#### Optional Parameters:
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/findOneDelete.png"alt="ToolJet - Mongo DB Find One and Delete"style={{marginBottom:'15px'}}/>
#### Example
```json
{
"order_id": 101
}
```
### Aggregate
Aggregation operations are expressions you can use to produce reduced and summarized results. [Reference](https://docs.mongodb.com/drivers/node/v4.0/fundamentals/aggregation/)
#### Required Parameters:
- **Collection**
- **Pipeline**
#### Optional Parameters:
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/aggregate.png"alt="ToolJet - Mongo DB Aggregate"style={{marginBottom:'15px'}}/>
#### Example
```json
[
{
"$match": {
"status": "completed"
}
},
{
"$group": {
"_id": "$product_id",
"totalSales": {
"$sum": "$amount"
}
}
}
]
```
### Delete One
Delete a record based on the filter. [Reference](https://docs.mongodb.com/drivers/node/v4.0/usage-examples/deleteOne/)
#### Required Parameters:
- **Collection**
- **Filter**
#### Optional Parameters:
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/deleteOne.png"alt="ToolJet - Mongo DB Find One"style={{marginBottom:'15px'}}/>
#### Example
```json
{
"user_id": 123
}
```
### Delete Many
Delete many records based on the filter. [Reference](https://docs.mongodb.com/drivers/node/v4.0/usage-examples/deleteMany/)
#### Required Parameters:
- **Collection**
- **Filter**
#### Optional Parameters:
- **Option**
<imgclassName="screenshot-full"src="/img/datasource-reference/mongo-db/deleteMany.png"alt="ToolJet - Mongo DB Find One"style={{marginBottom:'15px'}}/>