Cosmos DB
Read and write Azure Cosmos DB items with SQL queries, partition keys and master key or resource token authentication. Typical uses: read an Azure application's data, load rows into a container.
cosmos databasescosmos collectionscosmos collection_infocosmos putcosmos putcosmos insertcosmos insertcosmos getcosmos getcosmos querycosmos querycosmos findcosmos findcosmos countcosmos countcosmos updatecosmos updatecosmos deletecosmos deletecosmos delete_wherecosmos auth

cosmos databases <config>


Description

    The databases of the account

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
admin
cosmos databases "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}"
mentdb
["shop"]

cosmos collections <config>


Description

    The containers of the database

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
admin
cosmos collections "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}"
mentdb
["customers"]

cosmos collection_info <config> <container>


Description

    The definition of a container (partition key, indexing policy)

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
admin
cosmos collection_info "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers"
mentdb
{"id": "customers", "partitionKey": {"paths": ["/city"], "kind": "Hash"}}

cosmos put <config> <container> <id> <json_doc>


Description

    Upsert an item by id: the item

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    id:   The item id (no / \ # ? or control character) - String - required
    json_doc:   The document as a JSON object - String - required
admin
cosmos put "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "a1" "{\"name\": \"Zoé\", \"city\": \"Paris\", \"age\": 31}"
mentdb
{"id": "a1", "name": "Zoé", "city": "Paris", "age": 31}

cosmos put <config> <container> <id> <json_doc> <json_options>


Description

    Upsert with options: partition_key, system

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    id:   The item id (no / \ # ? or control character) - String - required
    json_doc:   The document as a JSON object - String - required
    json_options:   The options - String - required
admin
cosmos put "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "a2" "{\"name\": \"Paul\", \"city\": \"Paris\", \"age\": 45}" "{\"partition_key\": \"Paris\"}"
mentdb
{"id": "a2", "name": "Paul", "city": "Paris", "age": 45}

cosmos insert <config> <container> <json_rows>


Description

    Upsert rows one by one (the id field, else a random UUID): {inserted, errors, ids}; an item without its partition key goes to the partition None

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    json_rows:   The rows: a JSON array of objects (100000 at most) - String - required
admin
cosmos insert "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "[{\"id\": \"a3\", \"name\": \"Ana\", \"city\": \"Nice\", \"age\": 19}, {\"id\": \"a4\", \"name\": \"Max\"}]"
mentdb
{"inserted": 2, "errors": [], "ids": ["a3", "a4"]}

cosmos insert <config> <container> <json_rows> <json_options>


Description

    Insert with options: id_field (a field copied to id)

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    json_rows:   The rows: a JSON array of objects (100000 at most) - String - required
    json_options:   The options - String - required
admin
cosmos insert "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "[{\"code\": \"a5\", \"name\": \"Léa\", \"city\": \"Lyon\"}]" "{\"id_field\": \"code\"}"
mentdb
{"inserted": 1, "errors": [], "ids": ["a5"]}

cosmos get <config> <container> <id>


Description

    An item; null if it does not exist (a cross-partition query on the id)

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    id:   The item id (no / \ # ? or control character) - String - required
admin
cosmos get "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "a1"
mentdb
{"id": "a1", "name": "Zoé", "city": "Paris", "age": 31}

cosmos get <config> <container> <id> <json_options>


Description

    An item with options: Options: partition_key (a text, or a JSON array for a number, a hierarchical key or None, like [42] or [{}]); without it the item is found by a cross-partition query, system

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    id:   The item id (no / \ # ? or control character) - String - required
    json_options:   The options - String - required
admin
cosmos get "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "a1" "{\"partition_key\": \"Paris\"}"
mentdb
{"id": "a1", "name": "Zoé", "city": "Paris", "age": 31}

cosmos query <config> <container> <sql>


Description

    Run a SQL query: the items as rows (1000 at most, the continuation is followed)

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    sql:   The SQL query - String - required
admin
cosmos query "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "SELECT * FROM c"
mentdb
[{"id": "a1", "name": "Zoé", "city": "Paris", "age": 31}, {"id": "a2", "name": "Paul", "city": "Paris", "age": 45}, {"id": "a3", "name": "Ana", "city": "Nice", "age": 19}, {"id": "a4", "name": "Max"}, {"id": "a5", "code": "a5", "name": "Léa", "city": "Lyon"}]

cosmos query <config> <container> <sql> <json_options>


Description

    SQL with options: parameters ({"@city": "Paris"}), partition_key, cross_partition (true), limit (1000, 100000 at most), page_size (1000), system

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    sql:   The SQL query - String - required
    json_options:   The options - String - required
admin
cosmos query "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "SELECT * FROM c WHERE c.city = @city" "{\"parameters\": {\"@city\": \"Paris\"}, \"partition_key\": \"Paris\"}"
mentdb
[{"id": "a1", "name": "Zoé", "city": "Paris", "age": 31}, {"id": "a2", "name": "Paul", "city": "Paris", "age": 45}]

cosmos find <config> <container> <json_filter>


Description

    The items matching a filter (SQL with parameters) as rows

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    json_filter:   The filter, the same for every NoSQL family: {"city": "Paris", "age": {"$gte": 18}}; operators $eq $ne $gt $gte $lt $lte $in $nin; the conditions are combined with AND - String - required
admin
cosmos find "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "{\"age\": {\"$gte\": 20}}"
mentdb
[{"id": "a1", "name": "Zoé", "city": "Paris", "age": 31}, {"id": "a2", "name": "Paul", "city": "Paris", "age": 45}, {"id": "a3", "name": "Ana", "city": "Nice", "age": 19}, {"id": "a4", "name": "Max"}, {"id": "a5", "code": "a5", "name": "Léa", "city": "Lyon"}]

cosmos find <config> <container> <json_filter> <json_options>


Description

    Find with options: limit, sort (with partition_key), fields, partition_key, system, page_size

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    json_filter:   The filter, the same for every NoSQL family: {"city": "Paris", "age": {"$gte": 18}}; operators $eq $ne $gt $gte $lt $lte $in $nin; the conditions are combined with AND - String - required
    json_options:   The options - String - required
admin
cosmos find "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "{}" "{\"limit\": 1}"
mentdb
[{"id": "a1", "name": "Zoé", "city": "Paris", "age": 31}]

cosmos count <config> <container>


Description

    The number of items (SELECT VALUE COUNT(1))

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
admin
cosmos count "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers"
mentdb
5

cosmos count <config> <container> <json_filter>


Description

    The number of items matching a filter

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    json_filter:   The filter, the same for every NoSQL family: {"city": "Paris", "age": {"$gte": 18}}; operators $eq $ne $gt $gte $lt $lte $in $nin; the conditions are combined with AND - String - required
admin
cosmos count "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "{\"city\": \"Paris\"}"
mentdb
5

cosmos update <config> <container> <json_filter> <json_changes>


Description

    Set top-level fields in the matching items, each replaced with its _etag (an item changed meanwhile is not modified): {matched, modified}

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    json_filter:   The filter, the same for every NoSQL family: {"city": "Paris", "age": {"$gte": 18}}; operators $eq $ne $gt $gte $lt $lte $in $nin; the conditions are combined with AND - String - required
    json_changes:   The new values of top-level fields, like {"vip": true} - String - required
admin
cosmos update "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "{\"city\": \"Paris\"}" "{\"vip\": true}"
mentdb
{"matched": 5, "modified": 5}

cosmos update <config> <container> <json_filter> <json_changes> <json_options>


Description

    Update with options: limit (100000)

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    json_filter:   The filter, the same for every NoSQL family: {"city": "Paris", "age": {"$gte": 18}}; operators $eq $ne $gt $gte $lt $lte $in $nin; the conditions are combined with AND - String - required
    json_changes:   The new values of top-level fields, like {"vip": true} - String - required
    json_options:   The options - String - required
admin
cosmos update "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "{}" "{\"checked\": true}" "{\"limit\": 1}"
mentdb
{"matched": 1, "modified": 1}

cosmos delete <config> <container> <id>


Description

    Delete an item: {deleted: 1}, or {deleted: 0}

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    id:   The item id (no / \ # ? or control character) - String - required
admin
cosmos delete "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "a3"
mentdb
{"deleted": 1}

cosmos delete <config> <container> <id> <json_options>


Description

    Delete with options: partition_key

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    id:   The item id (no / \ # ? or control character) - String - required
    json_options:   The options - String - required
admin
cosmos delete "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "a2" "{\"partition_key\": \"Paris\"}"
mentdb
{"deleted": 1}

cosmos delete_where <config> <container> <json_filter>


Description

    Delete the items matching a filter: {deleted}

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    container:   The container (the collection of Cosmos DB) - String - required
    json_filter:   The filter, the same for every NoSQL family: {"city": "Paris", "age": {"$gte": 18}}; operators $eq $ne $gt $gte $lt $lte $in $nin; the conditions are combined with AND - String - required
admin
cosmos delete_where "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_key\", \"database\": \"shop\"}" "customers" "{}"
mentdb
{"deleted": 3}

cosmos auth <config> <verb> <resource_type> <resource_link>


Description

    The Authorization, x-ms-date and x-ms-version headers of a request signed with the master key, without any network call (to call the REST API with rest request, or to check a signature)

Parameters

    config:   The Cosmos DB configuration as JSON: endpoint (https://.documents.azure.com:443/), key (the primary or secondary key: HMAC-SHA256 signature of each request) or resource_token, database, allow_plain (false), timeout_ms (30000), max_bytes (64 MB), now (a fixed date for tests, like Thu, 27 Apr 2017 00:51:12 GMT) - String - required
    verb:   GET, POST, PUT, DELETE, PATCH or HEAD - String - required
    resource_type:   dbs, colls, docs, sprocs, udfs, triggers, users, permissions, attachments, pkranges or offers - String - required
    resource_link:   The resource link, like dbs/ToDoList (empty for the list of databases) - String - required
admin
cosmos auth "{\"endpoint\": \"https://myaccount.documents.azure.com:443/\", \"key\": \"cosmos_doc_key\", \"now\": \"Thu, 27 Apr 2017 00:51:12 GMT\"}" "GET" "dbs" "dbs/ToDoList"
mentdb
{"authorization": "type%3Dmaster%26ver%3D1.0%26sig%3Dc09PEVJrgp2uQRkr934kFbTqhByc7TVr3OHyqlu%2Bc%2Bc%3D", "x-ms-date": "Thu, 27 Apr 2017 00:51:12 GMT", "x-ms-version": "2018-12-31"}