# MongoDB

**MongoDB** makes operations in a Mongo database connection, returning only one JSON object.

{% hint style="info" %}
Mind the memory consumption for big datasets.
{% endhint %}

## **Parameters**

Take a look at the configuration parameters for the connector. Parameters supported by [Double Braces expressions](https://docs.digibee.com/documentation/connectors-and-triggers/double-braces/overview) are marked with `(DB)`.

### **General tab**

<table data-full-width="true"><thead><tr><th>Parameter</th><th>Description</th><th>Default value</th><th>Data type</th></tr></thead><tbody><tr><td><strong>Account</strong></td><td>Account to be used by the connector. Supported accounts: <strong>Basic</strong> and <strong>Certificate Chain</strong>.</td><td>N/A</td><td>String</td></tr><tr><td><strong>Use SSL/TLS to connect</strong></td><td>When activated, a secure SSL/TLS connection will be used.</td><td>False</td><td>Boolean</td></tr><tr><td><strong>Custom SSL/TLS certificate</strong></td><td>Sets the custom certificate that can be used in Double Braces expressions for the secure connection.</td><td>N/A</td><td>String</td></tr><tr><td><strong>Allow invalid hostnames</strong></td><td>When activated, the option bypasses the validation of hostnames in SSL/TLS certificates.</td><td>False</td><td>Boolean</td></tr><tr><td><strong>Operation</strong></td><td>Operation to be executed (<strong>Find, Aggregate</strong>, <strong>Delete One</strong>, <strong>Delete Many</strong>, <strong>Insert One</strong>, <strong>Insert Many</strong>, <strong>Update One</strong>, <strong>Update Many</strong>, <strong>Replace One</strong>, <strong>List Indexes</strong>, <strong>Create Index</strong>, and <strong>Drop Index</strong>).</td><td>Find</td><td>String</td></tr><tr><td><strong>Connection String</strong></td><td>The connection string. This parameter retrieves <code>globals</code> and <code>capsules</code> using the Double Braces expression but does not retrieve the <code>message</code> or <code>metadata</code> objects.</td><td><code>mongodb://localhost:27017</code></td><td>String</td></tr><tr><td><strong>Database Name</strong></td><td>Name of the database.</td><td><code>databaseName</code></td><td>String</td></tr><tr><td><strong>Collection Name</strong></td><td>Name of the collection.</td><td><code>collectionName</code></td><td>String</td></tr><tr><td><strong>Expire after seconds</strong></td><td>Time (in seconds) for documents expiration when using a TTL index. Only available if the <strong>Create Index</strong> operation is selected.</td><td>0</td><td>Integer</td></tr><tr><td><strong>Query</strong> <code>(DB)</code></td><td>Mongo specification to be used. For example: <code>{ _id: ObjectId( {{ message.$.id }} ) }</code></td><td>N/A</td><td>String</td></tr><tr><td><strong>Document</strong></td><td>Available only if <strong>Insert One</strong>, <strong>Insert Many</strong>, <strong>Update One</strong>, <strong>Update Many</strong>, or <strong>Replace One</strong> are selected.</td><td>N/A</td><td>String</td></tr><tr><td><strong>Limit</strong> <code>(DB)</code></td><td>Specification of the maximum number of objects that can be returned.</td><td>0</td><td>Integer</td></tr><tr><td><strong>Skip</strong> <code>(DB)</code></td><td>Number of objects to be skipped before returning to the query.</td><td>0</td><td>Integer</td></tr><tr><td><strong>Sort</strong></td><td>Specification of the parameter to be sorted by the field.</td><td>N/A</td><td>String</td></tr><tr><td><strong>Fail On Error</strong></td><td>If the option is activated, the pipeline's execution with an error will be interrupted. Otherwise, the pipeline execution proceeds, but the result will show a false value for the <code>"success"</code> property.</td><td>False</td><td>Boolean</td></tr><tr><td><strong>Max Wait For Connection (in ms)</strong></td><td>Defaults to 10000 (you may choose your option).</td><td>10000</td><td>Integer</td></tr><tr><td><strong>Connection Timeout (in ms)</strong></td><td>30000 (you may choose here).</td><td>30000</td><td>Integer</td></tr><tr><td><strong>Socket Timeout (in ms)</strong></td><td>30000, or another value.</td><td>300000</td><td>Integer</td></tr><tr><td><strong>Heartbeat Connection Timeout (in ms)</strong></td><td>10000 (you can determine your choice).</td><td>10000</td><td>Integer</td></tr><tr><td><strong>Max Connection Idle Timeout (in ms)</strong></td><td>Defaults to 1800000.</td><td>1800000</td><td>Integer</td></tr></tbody></table>

### **Documentation tab**

<table data-full-width="true"><thead><tr><th>Parameter</th><th>Description</th><th>Default value</th><th>Data type</th></tr></thead><tbody><tr><td><strong>Documentation</strong></td><td>Section for documenting any necessary information about the connector configuration and business rules.</td><td>N/A</td><td>String</td></tr></tbody></table>

{% hint style="info" %}
Currently, the connector only supports **Basic** and **Certificate Chain** accounts, and it must be informed through the **Account** field, not directly in the connection string.
{% endhint %}

You can:

* Use a fixed JSON: `document = "{\"data\": [{\"object\": 1}, {\"object\": 2}]}"`
* get some JSON of the message, that will search the `'data'` object of the message: `document = "{{ message.$.data }}`
* combine both examples: `document = "{\"data\": [{\"object\": {{ message.$.id1 }}}, {\"object\": {{ message.$.id2 }}}]}"]`

If MongoDB needs some authentication, you must create an account (BASIC type) and use it in the connetor.

To convert Double Braces, we use JSON Path specifications. [Read the documentation about JSON Path on GitHub](https://github.com/json-path/JsonPath).

## **Mongo DB in action**

<details>

<summary><strong>Operation Find</strong></summary>

**Config**

```json
{
	"operation": "FIND",
	"databaseName": "test",
	"collectionName": "model",
	"url": "mongodb://localhost:27017",
	"query": "{_id: ObjectId({{ message.$.parameters.id }})}",
	"failOnError": false
}
```

**Input**

```json
{
	"parameters": {
		"id": "5c87c7af06c3af7dbedc7bb3"
	}
}
```

**Output**

```json
{
	"data": [...some data...],
	"rowCount": 10,
	"updateCount": 0
}
```

</details>

<details>

<summary><strong>Operation Replace One</strong></summary>

**Config**

```json
{
	"operation": "REPLACE_ONE",
	"databaseName": "test",
	"collectionName": "model",
	"document": "{\"data\": [{\"object\": 1}, {\"object\": 2}]}",
	"url": "mongodb://localhost:27017",
	"query": "{_id: ObjectId({{ message.$.parameters.id }})}",
	"failOnError": false
}
```

**Input**

```json
{
	"parameters": {
		"id": "5c87c7af06c3af7dbedc7bb3"
	}
}
```

**Output**

```json
{
    "data": {},
    "rowCount": 0,
    "updateCount": 1
}
```

</details>

<details>

<summary><strong>Operation Update</strong></summary>

**Config**

```json
{
	"operation": "UPDATE",
	"databaseName": "test",
	"collectionName": "model",
	"document": "{\"$set\": {\"data\": [{\"object\": 1}, {\"object\": 2}]}}",
	"url": "mongodb://localhost:27017",
	"query": "{_id: ObjectId({{ message.$.parameters.id }})}",
	"failOnError": false
}	
```

**Input**

```json
{
	"parameters": {
		"id": "5c87c7af06c3af7dbedc7bb3"
	}
}
```

**Output**

```json
{
	"data": {},
	"rowCount": 0,
	"updateCount": 1
}
```

</details>

<details>

<summary><strong>Operation Update Many</strong></summary>

**Config**

```json
{
	"operation": "UPDATE_MANY",
	"databaseName": "test",
	"collectionName": "model",
	"document": "{\"$set\": {\"data\": [{\"object\": 1}, {\"object\": 2}]}}",
	"url": "mongodb://localhost:27017",
	"query": "{name: ObjectId({{ message.$.parameters.name }})}",
	"failOnError": false
}
```

**Input**

```json
{
	"parameters": {
		"name": "NAME"
	}
}
```

**Output**

```json
{
	"data": {},
	"rowCount": 0,
	"updateCount": 1
}  
```

</details>

<details>

<summary><strong>Operation Delete</strong></summary>

**Config**

```json
{
	"operation": "DELETE",
	"databaseName": "test",
	"collectionName": "model",
	"url": "mongodb://localhost:27017",
	"query": "{_id: ObjectId({{ message.$.parameters.id }})}",
	"failOnError": false
}
```

**Input**

```json
{
	"parameters": {
		"id": "5c87c7af06c3af7dbedc7bb3"
	}
}
```

**Output**

```json
{
	"data": {},
	"rowCount": 10,
	"updateCount": 0
}
```

</details>

<details>

<summary><strong>Operation Delete Many</strong></summary>

**Config**

```json
{
	"operation": "DELETE_MANY",
	"databaseName": "test",
	"collectionName": "model",
	"url": "mongodb://localhost:27017",
	"query": "{name: {{ message.$.data.name }}}",
	"failOnError": false
}
```

**Input**

```json
{
	"data": {
		"name": "NAME"
	}
}
```

**Output**

```json
{
	"data": {},
	"rowCount": 10,
	"updateCount": 0
}
```

</details>

<details>

<summary><strong>Operation Insert</strong></summary>

**Config**

```json
{
	"operation": "INSERT",
	"databaseName": "test",
	"collectionName": "model",
	"document": "{\"data\": {{ message.$.body }}}",
	"url": "mongodb://localhost:27017",
	"failOnError": false
}
```

**Input**

```json
{
	"parameters": {
		"id": "5c87c7af06c3af7dbedc7bb3"
	},
	"body": [
		{"a": 1},
		{"a": 2}
	]
}
```

**Output**

```json
{
	"data": {},
	"rowCount": 0,
	"updateCount": 1
}
```

</details>

<details>

<summary><strong>Operation Insert Many</strong></summary>

**Config**

```json
{
	"operation": "INSERT_MANY",
	"databaseName": "test",
	"collectionName": "model",
	"document": "{{ message.$.body }}",
	"url": "mongodb://localhost:27017",
	"failOnError": false
}
```

**Input**

```json
{
	"parameters": {
		"id": "5c87c7af06c3af7dbedc7bb3"
	},
	"body": [
		{"a": 1},
		{"a": 2}
	]
}
```

**Output**

````json
{
	"data": {},
	"rowCount": 0,
	"updateCount": 1
}```
````

</details>

<details>

<summary><strong>Operation Aggregate</strong></summary>

**Config**

```json
{
	"operation": "AGGREGATE",
	"databaseName": "test",
	"collectionName": "model",
	"url": "mongodb://localhost:27017",
	"query": "[{\"$match\":{\"zip\":\"90210\"}},{\"$group\":{\"_id\":null,\"count\":{\"$sum\":1}}}]",
	"failOnError": false
}
```

**Input**

```json
{
	"parameters": {
		"id": "5c87c7af06c3af7dbedc7bb3"
	}
}
```

**Output**

```json
{
	"data": [...some data...],
	"rowCount": 10,
	"updateCount": 0
}
```

</details>

<details>

<summary><strong>Operation List Indexes</strong></summary>

**Config**

```json
{
	"operation": "LIST_INDEXES",
	"databaseName": "test",
	"collectionName": "model",
	"url": "mongodb://localhost:27017",
	"failOnError": false
}
```

**Input**

```json
{ }
```

**Output**

```json
{
	"data": [...some data...],
	"rowCount": 10
}  
```

</details>

<details>

<summary><strong>Operation Create Index</strong></summary>

**Config**

```json
{
	"operation": "CREATE_INDEX",
	"databaseName": "test",
	"collectionName": "model",
	"url": "mongodb://localhost:27017",
	"expireAfterSeconds": "3600",
	"query": "{ \"createdAt\": 1 }",
	"failOnError": false
}
```

**Input**

```json
{ }
```

**Output**

```json
{
	"data": "createdAt_1",
	"updateCount": 1
}
```

</details>

<details>

<summary><strong>Operation Drop Index</strong></summary>

**Config**

```json
{
	"operation": "DROP_INDEX",
	"databaseName": "test",
	"collectionName": "model",
	"url": "mongodb://localhost:27017",
	"query": "{ \"createdAt\": 1 }",
	"failOnError": false
}
```

**Input**

```json
{ }
```

**Output**

```json
{
	"data": { },
	"updateCount": 1
}
```

</details>
