Introduction to the NoSQL API

Learn the essential expression to query Cosmos DB on NoSQL and the limitations and differences with traditional RDBMS’s SQL.

SQL in a NoSQL database

For those unfamiliar with the terminology, using SQL in a NoSQL database might sound strange. However, NoSQL means Not Only SQL, and even if databases like MongoDB ditched it for a custom query language, Cosmos DB embraces it as its primary query language.

What is it?

The Cosmos DB NoSQL API is the primary way to query the database. It closely represents the underlying document architecture, while other APIs must project it.

Microsoft has complete control over the interface, service, and SDKs, so every new feature is first available on this API.

As said above, the NoSQL API uses SQL syntax, the most popular query language. However, the lack of relationships and the nature of JSON documents make it impossible to support the SQL we might already know fully.

When to use it?

Being the native API, it should always be the primary choice. All other APIs exist to migrate an existing project (or team) to Cosmos DB, with a minor exception for the Gremlin API, which transforms it into a graph database. Also, knowing what we can do with this API might be helpful because it exposes its features and limits when using the other ones. For these reasons, the NoSQL API is the main API we will use during this course.

Let’s now jump into some examples!

Playground

We can test every query in this lesson with the terminal available in each section.

To use the terminal (first time):

  • Log in to the Azure portal using the az login command.

  • To get the connection string to the Cosmos DB account, you can do either of the following:

    • Run az cosmosdb keys list --name <your-account-name> --resource-group <your-resourceGroup> --type connection-strings ;

    • Check in the account’s “Keys” section in the Azure portal.

  • Set the CONNECTION_STRING environment variable before starting the terminal.

On launch, the program does the following:

  • Creates an educative database if it doesn’t exist.

  • Creates a movies container if it doesn’t exist.

  • Uploads the list of movies Disney published.

Each document has the following fields:

Get hands-on with 1400+ tech skills courses.