> For the complete documentation index, see [llms.txt](https://docs.octoxlabs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.octoxlabs.com/adapters/m/mongodb-adapter.md).

# MongoDB Adapter

MongoDB is a document database. OctoXLabs reads documents from a MongoDB view or collection that you prepare and adds each document to the device inventory.

## Use cases

* List the devices read from your MongoDB view or collection.

```
Adapters = mongodb-adapter
```

## Before you begin

### Network

| Source              | Destination                                                | Port                          | Protocol                    |
| ------------------- | ---------------------------------------------------------- | ----------------------------- | --------------------------- |
| OctoXLabs connector | MongoDB host (or the hosts in the Atlas connection string) | 27017 (default, configurable) | TCP (MongoDB wire protocol) |

### Required permissions

| Scope or role | Permission | Needed for                                                  |
| ------------- | ---------- | ----------------------------------------------------------- |
| Read          | Read       | The database, and the view or collection that you configure |

The frontend instructions do not list roles. The user must be able to read the configured view or collection. Not verified which built-in role is needed.

## Create credentials in MongoDB Adapter

{% stepper %}
{% step %}

### Create a view

OctoXLabs needs a view. In the view, name the column that holds the host name `Hostname`. Add the other fields as you wish. Alternatively, use a collection with a custom query.
{% endstep %}

{% step %}

### Create a user

Create a MongoDB user that can read the view or collection. Note the database in which the user is created (the authentication database).
{% endstep %}

{% step %}

### Atlas only

Copy the full connection string from the Atlas dashboard. It starts with `mongodb+srv://`. For local MongoDB, leave the connection string empty.
{% endstep %}
{% endstepper %}

## Connection parameters

| Parameter                                           | Required | Type    | Default | Description                                                                                                                                                                                        |
| --------------------------------------------------- | -------- | ------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| IP (`ip`)                                           | Yes      | String  |         | IP address or host name of the MongoDB host.                                                                                                                                                       |
| Port (`port`)                                       | No       | Integer | 27017   | MongoDB port.                                                                                                                                                                                      |
| Connection String (`connection_string`)             | No       | String  |         | Full connection string. Needed for MongoDB Atlas. Leave empty for local MongoDB.                                                                                                                   |
| Database (`database`)                               | Yes      | String  |         | Database to connect to.                                                                                                                                                                            |
| Authentication Database (`authentication_database`) | No       | String  |         | Database in which the user is created (`authSource`). When empty, the Database value is used.                                                                                                      |
| Username (`username`)                               | No       | String  |         | User name.                                                                                                                                                                                         |
| Password (`password`)                               | No       | String  |         | Password of the user.                                                                                                                                                                              |
| View Name (`view_name`)                             | No       | String  |         | View to read. Used when there is no valid custom query.                                                                                                                                            |
| Custom Query (`custom_query`)                       | No       | JSON    |         | Query for the collection, for example `{ "hostname": { "$in": ["server1.example.com"] } }`. Only these operators are allowed: $eq, $gt, $lt, $gte, $lte, $ne, $in, $nin, $exists, $and, $or, $not. |
| Collection Name (`collection_name`)                 | No       | String  |         | Collection to query with the custom query, for example `servers`.                                                                                                                                  |

## Troubleshooting

| Error message                                                | Cause                                                                                                                     | Fix                                                                  |
| ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------- |
| It is mandatory to enter either a valid custom query or view | No view name is set and the custom query is empty, not a JSON object, uses a blocked operator, or has no collection name. | Set a view name, or set a valid custom query with a collection name. |

## Change history

{% hint style="info" %}
Available starting from version 5.4.2.
{% endhint %}
