# MongoDB

## Setup

The following credentials keys are accepted:

* `host` **(required)** -> The hostname / ip of the instance
* `user` **(required)** -> The username to access the instance
* `password` (optional) -> The password to access the instance
* `port` (optional) -> The port of the instance. Default is `27017`.
* `tls` (optional) -> whether to use TLS for connecting (`true`/`false`).
* `cert_file` (optional) -> the client certificate to use to access the instance via TLS (file path or raw)
* `cert_key_file` (optional) -> the client key to use to access the instance via TLS (file path or raw)
* `cert_ca_file` (optional) -> the client CA certificate to use to access the instance via TLS (file path or raw)

### Using `sling conns`

Here are examples of setting a connection named `MONGODB`. We must provide the `type=mongodb` property:

{% code overflow="wrap" %}

```bash
$ sling conns set MONGODB type=mongodb host=<host> user=<user> password=<password> port=<port> 
```

{% endcode %}

### Environment Variable

See [here](https://docs.slingdata.io/connections/database-connections/pages/eAdVs2BHCgdr6RS8GoJC#dot-env-file-.env.sling) to learn more about the `.env.sling` file.

{% code overflow="wrap" %}

```bash
# see https://www.mongodb.com/docs/manual/reference/connection-string-options/#std-label-connections-connection-options for URL options
export MONGODB='mongodb://root:password@host.ip:27017?authSource=<database>'
export MONGODB='{ type: mongodb, user: "root", password: "password", host: "host.ip", port: 27017, authSource: "<database>" }'
```

{% endcode %}

### Sling Env File YAML

See [here](https://docs.slingdata.io/connections/database-connections/pages/eAdVs2BHCgdr6RS8GoJC#sling-env-file-env.yaml) to learn more about the sling `env.yaml` file.

```yaml
connections:
  MONGODB:
    type: mongodb
    host: host.ip
    port: 27017
    user: <user>
    password: <password>
    tls: true # if TLS connection needed

  # see https://www.mongodb.com/docs/manual/reference/connection-string-options/#std-label-connections-connection-options for URL options
  MONGODB_URL:
    type: mongodb
    url: mongodb://root:password@host.ip:27017?authSource=<database>

  MONGODB_TLS:
    type: mongodb
    url: mongodb://root:password@host.ip:27017?tls=true
```

## Examples

```yaml
source: mongodb
target: oracle

defaults:
  mode: full-refresh
  object: user1.{stream_schema}_{stream_table}

streams:
  db1.collection1:
  db1.collection2:
    select: [col1, col2]
    where: '{"field": "value", "field2": {"$gt": 100}}'  # use "where" starting v1.3.6
  db2.collection3:
    where: '[{"$or": [{"field1": "value1"}, {"field2": "value2"}]}]'  # use "where" starting v1.3.6
  db2.collection4:
    mode: incremental
    primary_key: [id]
    update_key: last_mod_at
  db2.collection5:
    mode: incremental
    primary_key: [id]
    where: '{"_id": {"$gte": "67859d8ee682ab32317abc6f", "$lte": "67859d8ee682ab32317abcc8"}}' # filter on ID (v1.4.21+)
    update_key: last_mod_at
```

If you are facing issues connecting, please reach out to us at <support@slingdata.io>, on [discord](https://discord.gg/q5xtaSNDvp) or open a Github Issue [here](https://github.com/slingdata-io/sling-cli/issues).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.slingdata.io/connections/database-connections/mongodb.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
