> For the complete documentation index, see [llms.txt](https://docs.cosmocloud.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cosmocloud.io/advanced-guide/mongodb-query-language.md).

# MongoDB Query Language

Cosmocloud is natively integrated with MongoDB as the primary database and can run native MongoDB Queries.

Custom MongoDB queries can be written supported in these nodes -

* [Custom Query Records](/flow-builder/node-types/database-nodes/run-aggregation-pipeline.md)
  * Supports Aggregation Pipelines
* [Update Record](/flow-builder/node-types/database-nodes/update-by-id.md)
  * Support MongoDB Update Query
* [Update Multiple Records](/flow-builder/node-types/database-nodes/update-many.md)
  * Support MongoDB Update Query
* [Delete Multiple Records](/flow-builder/node-types/database-nodes/delete-many.md)
  * Supports MongoDB Delete Query

{% hint style="info" %}
To learn more about MongoDB Query Language, you can check the official [documentation of MongoDB](https://docs.mongodb.com/manual).
{% endhint %}

## BSON to EJSON

*MongoDB Extended JSON (EJSON)* is a string format for representing BSON documents. This specification defines the canonical format for representing each BSON type in the Extended JSON format.

**Cosmocloud supports MongoDB Queries converted in Extended JSON format**. You need to convert some of the rich BSON features (such as `ObjectID`) to EJSON way to represent the same.

For example, you might have a sample aggregation query like this -

```javascript
[
    {
        "$match": {
                "paymentId": ObjectId("123456789012345678901234")
        }
    }
]
```

As JSON does not support custom classes like `ObjectId` and similar, you need to convert the same into EJSON format as below -

<pre class="language-json"><code class="lang-json"><strong>[
</strong>    {
        "$match": {
                "paymentId": {
                        "$oid": "123456789012345678901234"
                }
        }
    }
]
</code></pre>

As you can see, each special item in MongoDB query can be converted to EJSON structure, so that the system can easily read it and pass the same to MongoDB.

### Conversion table

<table><thead><tr><th width="275">BSON Type</th><th>Canonical Extended JSON Format</th></tr></thead><tbody><tr><td>ObjectId</td><td><p>{"$oid": &#x3C;ObjectId bytes as 24-character, big-endian <em>hex string</em>>}<br><br><em>Example -</em> </p><pre class="language-json"><code class="lang-json">{
    "$oid": "123456789012345678901234"
}
</code></pre></td></tr><tr><td>Regular Expression</td><td><p>{"$regularExpression": {pattern: <em>string</em>, "options": &#x3C;BSON regular expression options as a <em>string</em>>}}<br><br><em>Example -</em> </p><pre class="language-json"><code class="lang-json">{
    "$regexExpression": {
        "pattern": "something",
        "options": "i"
    }
}
</code></pre></td></tr><tr><td>Datetime Object<br>(Canonical form)</td><td><p>{"$date": {"$numberLong": &#x3C;epoch in millisec, as a <em>string</em>>}}<br><br><em>Example -</em> </p><pre class="language-json"><code class="lang-json">{
    "$date": {
        "$numberLong": "1713767901000"
    }
}
</code></pre></td></tr><tr><td>Datetime Object<br>(Relaxed form)</td><td><p>{"$date": "ISO Datetime Format with maximum time precision of milliseconds, as a <em>string</em>"}<br><br><em>Example -</em></p><pre class="language-json"><code class="lang-json">{
    "$date": "2024-04-22T06:38:21.125Z"
}
</code></pre></td></tr><tr><td>Boolean</td><td>true or false</td></tr><tr><td>Null</td><td>null</td></tr></tbody></table>

For more references, you can check the [official specification document](https://github.com/mongodb/specifications/blob/master/source/extended-json.rst#conversion-table).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.cosmocloud.io/advanced-guide/mongodb-query-language.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
