> ## Documentation Index
> Fetch the complete documentation index at: https://nocode-docs.zeroagent.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Learn to use Cosmos Database Operations

<img src="https://mintcdn.com/zeromagiclabsprivatelimited/7ggIiMFZ-j3juw2y/assets/blogs/learn-to-use-cosmos-database-operations/thumbnail.png?fit=max&auto=format&n=7ggIiMFZ-j3juw2y&q=85&s=2c000e0718c571f077f50d58b20eb19c" alt="thumbnail" width="1280" height="720" data-path="assets/blogs/learn-to-use-cosmos-database-operations/thumbnail.png" />

Do you need a highly scalable and globally distributed NoSQL database for your modern application? Look no further than Azure Cosmos DB!

In this blog post, we will dive into the various operations you can perform on Azure Cosmos DB using the Zeromagic platform. Whether you need to create, read, update, or delete data, Zeromagic provides a set of predefined actions to help you manage your database efficiently. These operations include CreateOne, CreateMany, ReadOne, ReadMany, DeleteOne, DeleteMany, UpdateOne, UpdateMany, and Custom Filter queries. Let's explore each operation in detail to understand how you can use them to manage your Cosmos DB effectively.

<Tip>
  Reference

  Check [here](/datasources/zeromagic-query-language/overview/) to learn more about Zeromagic Query Language
</Tip>

Login to [Zeromagic Platform](https://console.zeromagic.cloud/) and go to you `Project Console`. Now navigate to `Modules` in the sidebar of the console. Click on the necessary Module and navigate to the Flow Builder Page.

## 1. CreateOne

The `CreateOne` operation allows you to insert a single document into your Cosmos DB container. This is useful when you need to add individual records to your database.

1. Add a CreateOne block to your workflow. Set the `record to add` field to `{{$request.body}}` so as to specify the data that will be used to create the new record.
2. In the `Http Response` block , set the `Response Variable` field to expression as `{{$block-name.val}}`.

```bash API URL theme={null}
<base_url>/student/
```

<img src="https://mintcdn.com/zeromagiclabsprivatelimited/7ggIiMFZ-j3juw2y/assets/blogs/learn-to-use-cosmos-database-operations/createOne.png?fit=max&auto=format&n=7ggIiMFZ-j3juw2y&q=85&s=ced403da7e2735ff5eb576103e4cc357" alt="CreateOne" width="1200" height="661" data-path="assets/blogs/learn-to-use-cosmos-database-operations/createOne.png" />

### Sample Data

```json Request Body Data theme={null}
{
    "id": "1",
    "name": "John Doe",
    "email": "john.doe@example.com",
    "courses" : ["maths", "science", "english"],
    "dept" : "CSE"
}
```

```json Response Data theme={null}
{
  "data": {
    "id": "1",
    "_ts": 1726552985,
    "_rid": "5bQ3ANPWhcUHAAAAAAAAAA==",
    "dept": "CSE",
    "name": "John Doe",
    "_etag": "\"0000650d-0000-0300-0000-66e91b990000\"",
    "_self": "dbs/5bQ3AA==/colls/5bQ3ANPWhcU=/docs/5bQ3ANPWhcUHAAAAAAAAAA==/",
    "email": "john.doe@example.com",
    "_module": "rest_student",
    "courses": [
      "maths",
      "science",
      "english"
    ],
    "_attachments": "attachments/"
  }
}
```

## 2. CreateMany

The `CreateMany` operation enables you to insert multiple documents at once. This is ideal for batch operations where you need to add several records simultaneously.

1. Add a CreateMany block to your workflow. Set the `Create Value` field to `{{$request.body.<key>}}` so as to specify the data that will be used to create the new record.
2. In the `Http Response` block , set the `Response Variable` field to expression as `{{$block-name.val}}`.

```bash API URL theme={null}
<base_url>/student/bulk/
```

<img src="https://mintcdn.com/zeromagiclabsprivatelimited/7ggIiMFZ-j3juw2y/assets/blogs/learn-to-use-cosmos-database-operations/createMany.png?fit=max&auto=format&n=7ggIiMFZ-j3juw2y&q=85&s=281174744828c7156e7b2b1067201d6d" alt="CreateMany" width="1200" height="655" data-path="assets/blogs/learn-to-use-cosmos-database-operations/createMany.png" />

### Sample Data

```json Request Body Data theme={null}
 {
  "students": [
  {
    "id": "2",
    "name": "Jim Beam",
    "email": "jim.beam@example.com",
    "courses" : ["physics", "english"],
    "dept" : "ECE"
},
{
    "id": "3",
    "name": "Jane Doe",
    "email": "jane.doe@example.com",
    "courses" : ["maths", "science"],
    "dept" : "CSE"
} 
]
 }
```

```json Response Data theme={null}
{
  "data": [
    {
      "id": "2",
      "_ts": 1726553235,
      "_rid": "5bQ3ANPWhcUIAAAAAAAAAA==", 
      "dept": "ECE",
      "name": "Jim Beam",
      "_etag": "\"00006b0d-0000-0300-0000-66e91c930000\"",
      "_self": "dbs/5bQ3AA==/colls/5bQ3ANPWhcU=/docs/5bQ3ANPWhcUIAAAAAAAAAA==/",
      "email": "jim.beam@example.com",
      "_module": "rest_student",
      "courses": [
        "physics",
        "english"
      ],
      "_attachments": "attachments/"
    },
    {
      "id": "3",
      "_ts": 1726553235,
      "_rid": "5bQ3ANPWhcUJAAAAAAAAAA==", 
      "dept": "CSE",
      "name": "Jane Doe",
      "_etag": "\"00006c0d-0000-0300-0000-66e91c930000\"",
      "_self": "dbs/5bQ3AA==/colls/5bQ3ANPWhcU=/docs/5bQ3ANPWhcUJAAAAAAAAAA==/",
      "email": "jane.doe@example.com",
      "_module": "rest_student",
      "courses": [
        "maths",
        "science"
      ],
      "_attachments": "attachments/"
    }
  ]
}
```

## 3. ReadOne

The `ReadOne` operation allows you to retrieve a single document based on its unique identifier. This is useful when you need to fetch specific records.

1. Add a ReadOne block to your workflow. Set the `Record Id` field to `{{$request.body.id}}` so as to specify the data that will be used to create the new record.
2. In the `Http Response` block , set the `Response Variable` field to expression as `{{$block-name.val}}`.

```bash API URL theme={null}
<base_url>/student/{id}/
```

<img src="https://mintcdn.com/zeromagiclabsprivatelimited/7ggIiMFZ-j3juw2y/assets/blogs/learn-to-use-cosmos-database-operations/readOne.png?fit=max&auto=format&n=7ggIiMFZ-j3juw2y&q=85&s=11889c576c4e0040aa91b0df1f7e6e8e" alt="readOne" width="1200" height="651" data-path="assets/blogs/learn-to-use-cosmos-database-operations/readOne.png" />

### Sample Data

```json Request Data theme={null}
curl -X GET https://api.zeromagic.cloud/api/b819ea802f674352beec615db8297262/dev/operations/student/1/ \
-H "APP-KEY: YOUR_APP_KEY"
```

```json Response Data theme={null}
{
  "data": {
    "id": "1",
    "_ts": 1726552985,
    "_rid": "5bQ3ANPWhcUHAAAAAAAAAA==", 
    "dept": "CSE",
    "name": "John Doe",
    "_etag": "\"0000650d-0000-0300-0000-66e91b990000\"",
    "_self": "dbs/5bQ3AA==/colls/5bQ3ANPWhcU=/docs/5bQ3ANPWhcUHAAAAAAAAAA==/",
    "email": "john.doe@example.com",
    "_module": "rest_student",
    "courses": [
      "maths",
      "science",
      "english"
    ],
    "_attachments": "attachments/"
  }
}
```

## 4. ReadMany

The `ReadMany` operation lets you fetch multiple documents that match a given criteria. This is beneficial for retrieving a set of records based on certain conditions.

```bash API URL theme={null}
<base_url>/students/
```

1. Add a ReadMany block to your workflow.  Set the `Sort Key` field to **name**, which corresponds to the data key in the schema, and choose either `asc` or `desc` for the `Sort Order`, depending on how you want the data to be sorted.

2. In the `Http Response` block , set the `Response Variable` field to expression as `{{$block-name.val}}`.

<img src="https://mintcdn.com/zeromagiclabsprivatelimited/7ggIiMFZ-j3juw2y/assets/blogs/learn-to-use-cosmos-database-operations/readMany.png?fit=max&auto=format&n=7ggIiMFZ-j3juw2y&q=85&s=d55374b679ffe584c1f53dd7ca6670fb" alt="readMany" width="1200" height="663" data-path="assets/blogs/learn-to-use-cosmos-database-operations/readMany.png" />

### Sample Data

```json Response Data theme={null}
{
  "data": [
    {
      "id": "3",
      "_ts": 1726553235,
      "_rid": "5bQ3ANPWhcUJAAAAAAAAAA==", 
      "dept": "CSE",
      "name": "Jane Doe",
      "_etag": "\"00006c0d-0000-0300-0000-66e91c930000\"",
      "_self": "dbs/5bQ3AA==/colls/5bQ3ANPWhcU=/docs/5bQ3ANPWhcUJAAAAAAAAAA==/",
      "email": "jane.doe@example.com",
      "_module": "rest_student",
      "courses": [
        "maths",
        "science"
      ],
      "_attachments": "attachments/"
    },
    {
      "id": "2",
      "_ts": 1726553235,
      "_rid": "5bQ3ANPWhcUIAAAAAAAAAA==", 
      "dept": "ECE",
      "name": "Jim Beam",
      "_etag": "\"00006b0d-0000-0300-0000-66e91c930000\"",
      "_self": "dbs/5bQ3AA==/colls/5bQ3ANPWhcU=/docs/5bQ3ANPWhcUIAAAAAAAAAA==/",
      "email": "jim.beam@example.com",
      "_module": "rest_student",
      "courses": [
        "physics",
        "english"
      ],
      "_attachments": "attachments/"
    },
    {
      "id": "1",
      "_ts": 1726552985,
      "_rid": "5bQ3ANPWhcUHAAAAAAAAAA==", 
      "dept": "CSE",
      "name": "John Doe",
      "_etag": "\"0000650d-0000-0300-0000-66e91b990000\"",
      "_self": "dbs/5bQ3AA==/colls/5bQ3ANPWhcU=/docs/5bQ3ANPWhcUHAAAAAAAAAA==/",
      "email": "john.doe@example.com",
      "_module": "rest_student",
      "courses": [
        "maths",
        "science",
        "english"
      ],
      "_attachments": "attachments/"
    }
  ]
}
```

## 5. UpdateOne

The `UpdateOne` operation allows you to modify a single document. This is helpful when you need to update specific fields of a record.

1. Add a UpdateOne block to your workflow. Set the `Record Id` field to `{{$request.body.id}}` and `Update Value` to `{{$request.body.value}}` to specify the data that will be used to update the existing record.
2. In the `Http Response` block , set the `Response Variable` field to expression as `{{$block-name.val}}`.

```bash API URL theme={null}
<base_url>/student/put/{id}/
```

<img src="https://mintcdn.com/zeromagiclabsprivatelimited/7ggIiMFZ-j3juw2y/assets/blogs/learn-to-use-cosmos-database-operations/updateOne.png?fit=max&auto=format&n=7ggIiMFZ-j3juw2y&q=85&s=6f3d3eacba549751084754539cfbd139" alt="updateOne" width="1200" height="657" data-path="assets/blogs/learn-to-use-cosmos-database-operations/updateOne.png" />

### Sample Data

```json Request Data theme={null}
curl -X PUT https://api.zeromagic.cloud/api/b819ea802f674352beec615db8297262/dev/operations/student/put/1/ \
-H "APP-KEY: YOUR_APP_KEY" \
-d '{"age" : 21}'
```

```json Response Data theme={null}
{
  "data": {
    "id": "1",
    "_ts": 1727073910,
    "age": 21,
    "_rid": "5bQ3ANPWhcURAAAAAAAAAA==",
    "dept": "CSE",
    "name": "John Doe",
    "_etag": "\"1400bf92-0000-0300-0000-66f10e760000\"",
    "_self": "dbs/5bQ3AA==/colls/5bQ3ANPWhcU=/docs/5bQ3ANPWhcURAAAAAAAAAA==/",
    "email": "john.doe@example.com",
    "_module": "rest_student",
    "courses": [
      "maths",
      "science",
      "english"
    ],
    "_attachments": "attachments/"
  }
}
```

## 6. UpdateMany

The `UpdateMany` operation allows you to modify multiple documents. This is helpful when you need to update specific batch of records.

1. Add a UpdateMany block to your workflow. Set the `Filter Query` field to your desired query according to your data retrieval and `Update Value` to `{{$request.body}}` to specify the data that will be used to update the existing record.
2. In the `Http Response` block , set the `Response Variable` field to expression as `{{$block-name.val}}`.

```bash API URL theme={null}
<base_url>/students/put/bulk/
```

<img src="https://mintcdn.com/zeromagiclabsprivatelimited/7ggIiMFZ-j3juw2y/assets/blogs/learn-to-use-cosmos-database-operations/updateMany.png?fit=max&auto=format&n=7ggIiMFZ-j3juw2y&q=85&s=9cb76281bf8cb5b5819155cc1c8d8826" alt="updateMany" width="1200" height="595" data-path="assets/blogs/learn-to-use-cosmos-database-operations/updateMany.png" />

<img src="https://mintcdn.com/zeromagiclabsprivatelimited/7ggIiMFZ-j3juw2y/assets/blogs/learn-to-use-cosmos-database-operations/updateMany-filter.png?fit=max&auto=format&n=7ggIiMFZ-j3juw2y&q=85&s=8ee45746969b4106089dafc792fcf70c" alt="updateMany filter" width="750" height="328" data-path="assets/blogs/learn-to-use-cosmos-database-operations/updateMany-filter.png" />

### Sample Data

```json Request Body Data theme={null}
{
  "age": 20,
}
```

```json Response Data theme={null}
{
  "data": [
    {
      "id": "3",
      "_ts": 1727074102,
      "age": 20,
      "_rid": "5bQ3ANPWhcUTAAAAAAAAAA==",
      "dept": "CSE",
      "name": "Jane Doe",
      "_etag": "\"14008595-0000-0300-0000-66f10f360000\"",
      "_self": "dbs/5bQ3AA==/colls/5bQ3ANPWhcU=/docs/5bQ3ANPWhcUTAAAAAAAAAA==/",
      "email": "jane.doe@example.com",
      "_module": "rest_student",
      "courses": [
        "maths",
        "science"
      ],
      "_attachments": "attachments/"
    }
  ]
}

```

## 7. DeleteOne

The `DeleteOne` operation deletes a single document based on its unique identifier. This is useful for removing individual records such as deleting a user account.

1. Add a DeleteOne block to your workflow. Set the `Record Id` field to `{{$request.body.id}}` so as to specify the data that will be used to create the new record.
2. In the `Http Response` block , set the `Response Variable` field to expression as `{{$block-name.val}}`.

```bash API URL theme={null}
<base_url>/student/del/{id}/
```

<img src="https://mintcdn.com/zeromagiclabsprivatelimited/7ggIiMFZ-j3juw2y/assets/blogs/learn-to-use-cosmos-database-operations/deleteOne.png?fit=max&auto=format&n=7ggIiMFZ-j3juw2y&q=85&s=75825a1161168988d90f4df517509c26" alt="deleteOne" width="1200" height="593" data-path="assets/blogs/learn-to-use-cosmos-database-operations/deleteOne.png" />

### Sample Data

```json Request Data theme={null}
curl -X GET https://api.zeromagic.cloud/api/b819ea802f674352beec615db8297262/dev/operations/student/del/1/ \
-H "APP-KEY: YOUR_APP_KEY"
```

```json Response Data theme={null}
{
    "data": {
        "_attachments": "attachments/",
        "_etag": "\"14004e6b-0000-0300-0000-66f105130000\"",
        "_module": "rest_student",
        "_rid": "5bQ3ANPWhcUOAAAAAAAAAA==",
        "_self": "dbs/5bQ3AA==/colls/5bQ3ANPWhcU=/docs/5bQ3ANPWhcUOAAAAAAAAAA==/",
        "_ts": 1727071507,
        "age": 21,
        "courses": [
            "maths",
            "science",
            "english"
        ],
        "dept": "CSE",
        "email": "john.doe@example.com",
        "id": "1",
        "name": "John Doe"
    }
}
```

## 8. DeleteMany

The `DeleteMany` operation deletes multiple documents based on a query. This is ideal for bulk deletions such as removing all inactive accounts.

1. Add a DeleteMany block to your workflow. Set the `Delete Query` field to your desired query according to the data that will be used to delete the existing record.
2. In the `Http Response` block , set the `Response Variable` field to expression as `{{$block-name.val}}`.

```bash API URL theme={null}
<base_url>/students/del/bulk/
```

<img src="https://mintcdn.com/zeromagiclabsprivatelimited/7ggIiMFZ-j3juw2y/assets/blogs/learn-to-use-cosmos-database-operations/deleteMany.png?fit=max&auto=format&n=7ggIiMFZ-j3juw2y&q=85&s=a053db42a20289fdf43c06f40d88f245" alt="deleteMany" width="1200" height="587" data-path="assets/blogs/learn-to-use-cosmos-database-operations/deleteMany.png" />

<img src="https://mintcdn.com/zeromagiclabsprivatelimited/7ggIiMFZ-j3juw2y/assets/blogs/learn-to-use-cosmos-database-operations/deleteMany-filter.png?fit=max&auto=format&n=7ggIiMFZ-j3juw2y&q=85&s=0cf8ce6dcb8afc00c8c72280b307444b" alt="deleteMany-filter" width="745" height="270" data-path="assets/blogs/learn-to-use-cosmos-database-operations/deleteMany-filter.png" />

### Sample Data

```json Request Data theme={null}
curl -X GET https://api.zeromagic.cloud/api/b819ea802f674352beec615db8297262/dev/operations/students/del/bulk/ \
-H "APP-KEY: YOUR_APP_KEY"
```

```json Response Data theme={null}
{
  "data": [
    {
      "id": "2",
      "_ts": 1727071256,
      "age": 21,
      "_rid": "5bQ3ANPWhcUPAAAAAAAAAA==",
      "dept": "ECE",
      "name": "Jim Beam",
      "_etag": "\"1400b966-0000-0300-0000-66f104180000\"",
      "_self": "dbs/5bQ3AA==/colls/5bQ3ANPWhcU=/docs/5bQ3ANPWhcUPAAAAAAAAAA==/",
      "email": "jim.beam@example.com",
      "_module": "rest_student",
      "courses": [
        "physics",
        "english"
      ],
      "_attachments": "attachments/"
    },
    {
      "id": "3",
      "_ts": 1727070004,
      "age": 20,
      "_rid": "5bQ3ANPWhcUQAAAAAAAAAA==",
      "dept": "CSE",
      "name": "Jane Doe",
      "_etag": "\"1400b653-0000-0300-0000-66f0ff340000\"",
      "_self": "dbs/5bQ3AA==/colls/5bQ3ANPWhcU=/docs/5bQ3ANPWhcUQAAAAAAAAAA==/",
      "email": "jane.doe@example.com",
      "_module": "rest_student",
      "courses": [
        "maths",
        "science"
      ],
      "_attachments": "attachments/"
    }
  ]
}
```

## 9. CustomQuery

The `CustomQuery` operation allows for complex querying with custom filters. This is ideal for advanced data retrieval scenarios like filtering users by multiple criteria.

<Tip>
  Reference

  Check [here](/datasources/zeromagic-query-language/operators-property/) to learn more about Zeromagic Query Operators and Property.
</Tip>

1. Add a CustomQuery block to your workflow. Set the `Custom Aggregate Query` field to your desired query according to the data that will be used to manipulate the existing record.
2. In the `Http Response` block , set the `Response Variable` field to expression as `{{$block-name.val}}`.

<img src="https://mintcdn.com/zeromagiclabsprivatelimited/7ggIiMFZ-j3juw2y/assets/blogs/learn-to-use-cosmos-database-operations/customQuery.png?fit=max&auto=format&n=7ggIiMFZ-j3juw2y&q=85&s=b23c9632ec7ed88ccfe0906d4b602be9" alt="CustomQuery" width="1200" height="671" data-path="assets/blogs/learn-to-use-cosmos-database-operations/customQuery.png" />

Understanding and utilizing these operations will empower you to manage your Cosmos Database effectively, whether you need to create, read, update, or delete data. Our platform simplifies these tasks, enabling you to focus on building robust applications with ease.

Explore the power of Cosmos Database operations on our platform and elevate your data management capabilities today!
