# MongoDB

Canonical URL: https://developers.iagenify.com/sdk/ref/providers/mongodb/
Language: en

`api.storage.connect({ type: "database", provider: "mongodb", endpoint })`

A document database of your own. Nine read actions, four write actions, and the collections you already have — nothing is renamed and nothing is migrated.

Status: type: database · 9 reads · 4 writes · connection pooled

This is the richest of the connected providers, and the one the agent tools were shaped around: a database it has never seen can be mapped, queried and written to without a schema being declared anywhere.

## Connecting {#connect}

```javascript
const db = await api.storage.connect({
  label: "Production",
  type: "database",
  provider: "mongodb",
  endpoint: { credentials: { uri: "mongodb+srv://user:password@cluster.example.net/shop" } },
});
```

| Name | Type | Requirement | Description |
| --- | --- | --- | --- |
| `credentials.uri` | string | required | The connection string. It carries the password, so it belongs in `credentials`, which is encrypted at rest. |

> **The database name is the path of the URI**
>
> …/shop is what picks the database. A URI with no path lands on a default one, which reads as empty — that is the usual explanation for « my collections are missing ».

## What run() accepts {#actions}

| Action | Reads or writes | Needs collection |
| --- | --- | --- |
| [overview](/sdk/ref/connected-storage/run/overview) | read | no |
| [collections](/sdk/ref/connected-storage/run/collections) | read | no |
| [schema](/sdk/ref/connected-storage/run/schema) | read | yes |
| [find](/sdk/ref/connected-storage/run/find) | read | yes |
| [count](/sdk/ref/connected-storage/run/count) | read | yes |
| [aggregate](/sdk/ref/connected-storage/run/aggregate) | read | yes |
| [search](/sdk/ref/connected-storage/run/search) | read | no |
| [insert](/sdk/ref/connected-storage/run-write/insert) | write | yes |
| [update](/sdk/ref/connected-storage/run-write/update) | write | yes |
| [delete](/sdk/ref/connected-storage/run-write/delete) | write | yes |
| [save](/sdk/ref/connected-storage/run-write/save) | write | yes |
| [createCollection](/sdk/ref/connected-storage/run-write/create-collection) | write | yes |
| [dropCollection](/sdk/ref/connected-storage/run-write/drop-collection) | write | yes |

Anything else raises `Action MongoDB inconnue`. `tables`, `execute`, `list`, `object` and `upload` belong to other providers.

## _id may be a string {#objectid}

An identifier written as a 24-character string is turned into an ObjectId wherever one is expected: direct equality, `$in`, `$nin` and `$ne`. That is what lets an identifier travel through JSON — out of a `find`, into a form, back into an `update` — without a cast of yours in between.

```javascript
const [order] = await db.run({ action: "find", collection: "orders", options: { limit: 1 } });

// order._id arrived as a string; it goes back as one
await db.run({
  action: "update",
  collection: "orders",
  filter: { _id: order._id },
  update: { $set: { status: "shipped" } },
});
```

## Five operators are refused {#refused}

`$where`, `$function` and `$accumulator` run JavaScript inside the database; `$out` and `$merge` write somewhere other than where you are looking. All five are refused on `find`, `count`, `update`, `delete` and `aggregate`, by inspecting the filter or the pipeline you sent.

```javascript
await db.run({ action: "find", collection: "users", filter: { $where: "this.age > 30" } });
// Error: find : l'operateur $where n'est pas autorise.
```

## Bounds that are imposed {#limits}

| Thing | Bound |
| --- | --- |
| `aggregate` result | 500 documents |
| `aggregate` and `count` duration | 30 seconds |
| `insert` batch | 1 to 500 documents |
| `overview` | 30 collections, 25 fields, 1 sample each |
| `schema` | 20 recent documents, 150 fields |

## An empty filter is refused on a write {#empty-filter}

> **all: true is the way to say you meant it**
>
> update and delete with no filter would touch every document, so they stop unless you add all: true. Reads have no such guard — an empty filter on find is an ordinary query.

## The connection is kept {#pooling}

One client per URI is held and reused across calls, so the second `run()` does not reconnect. A URI that changes — a rotated password, a new host — is a new connection: it does not inherit the old one.

## System collections are filtered out {#system}

`collections` and `overview` leave out anything in the `system.` namespace. What you see is what you put there.

- [connect()](/sdk/ref/storage/connect): The call itself, and where the secret goes.
- [run()](/sdk/ref/connected-storage/run): Every action, and what each one takes.
- [IaGenify Cloud](/sdk/ref/providers/iagenify-cloud): The same shape, with no credentials to supply.

```python
db = api.storage.connect(
    label="Production",
    type="database",
    provider="mongodb",
    endpoint={"credentials": {"uri": "mongodb+srv://user:password@cluster.example.net/shop"}},
)
```

```python
order = db.run(action="find", collection="orders", options={"limit": 1})[0]

# order["_id"] arrived as a string; it goes back as one
db.run(
    action="update",
    collection="orders",
    filter={"_id": order["_id"]},
    update={"$set": {"status": "shipped"}},
)
```

```python
db.run(action="find", collection="users", filter={"$where": "this.age > 30"})
# IaGenifyError: find : l'operateur $where n'est pas autorise.
```
