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.

Reference

Check here to learn more about Zeromagic Query Language

Login to Zeromagic Platform 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}}.
API URL
<base_url>/student/

Sample Data

Request Body Data
{
    "id": "1",
    "name": "John Doe",
    "email": "john.doe@example.com",
    "courses" : ["maths", "science", "english"],
    "dept" : "CSE"
}
Response Data
{
  "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}}.
API URL
<base_url>/student/bulk/

Sample Data

Request Body Data
 {
  "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"
} 
]
 }
Response Data
{
  "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}}.
API URL
<base_url>/student/{id}/

Sample Data

Request Data
curl -X GET https://api.zeromagic.cloud/api/b819ea802f674352beec615db8297262/dev/operations/student/1/ \
-H "APP-KEY: YOUR_APP_KEY"
Response Data
{
  "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.

API URL
<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}}.

Sample Data

Response Data
{
  "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}}.
API URL
<base_url>/student/put/{id}/

Sample Data

Request Data
curl -X PUT https://api.zeromagic.cloud/api/b819ea802f674352beec615db8297262/dev/operations/student/put/1/ \
-H "APP-KEY: YOUR_APP_KEY" \
-d '{"age" : 21}'
Response Data
{
  "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}}.
API URL
<base_url>/students/put/bulk/

Sample Data

Request Body Data
{
  "age": 20,
}
Response Data
{
  "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}}.
API URL
<base_url>/student/del/{id}/

Sample Data

Request Data
curl -X GET https://api.zeromagic.cloud/api/b819ea802f674352beec615db8297262/dev/operations/student/del/1/ \
-H "APP-KEY: YOUR_APP_KEY"
Response Data
{
    "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}}.
API URL
<base_url>/students/del/bulk/

Sample Data

Request Data
curl -X GET https://api.zeromagic.cloud/api/b819ea802f674352beec615db8297262/dev/operations/students/del/bulk/ \
-H "APP-KEY: YOUR_APP_KEY"
Response Data
{
  "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.

Reference

Check here to learn more about Zeromagic Query Operators and Property.

  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}}.

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!