Professional Documents
Culture Documents
© 2018 Microsoft Corporation. All rights reserved. www.cosmosdb.com | Tweet: @AzureCosmosDB, #cosmosdb
Azure Cosmos DB Query Cheat Sheet
MongoDB API
Example: families collection Find a document by property
© 2018 Microsoft Corporation. All rights reserved. www.cosmosdb.com | Tweet: @AzureCosmosDB, #cosmosdb
Azure Cosmos DB Query Cheat Sheet
Table API
Query entities
Initialize a CloudTableClient Method GET
Preflight table request https://myaccount.table.cosmosdb.azure.com/mytable(Partitio-
TableconnectionPolicy connectionPolicy =
Key='<partition-key>',RowKey='<row-key>')?$select=<comma
new TableConnectionPolicy() MHTTP Verb OPTIONS
{ -separated-property-names>
UsedirectMode = true, http://myaccount.table.cosmosdb.azure.com/<table-resource>
https://myaccount.table.core.windows.net/mytable()?$fil-
UseTcpProtocol = true,
EnableEndpointdiscovery = true Request Headers ter=<query-expression>&$select=<comma-separated-propert
}; - Origin y-names>
CloudStorageAccount storageAccount =
- Access-Control-Request-Method
CloudStorageAccount.Parse(connectionString);
- Access-Control-Request-Headers Request Headers
CloudTableClient = storageAccount.CreateCloudTable
Client(connectionPolicy, Microsoft.Azure.Cosmos- - Authorization - Date or x-ms-date
DB.ConsistencyLevel.BoundedStaleness); - x-ms-version - Accept
Get table service stats - x-ms-client-request-id
Create a table Method GET
https://myaccount.table.cosmosdb.azure.com/?restype=ser-
Insert entity
CloudTable table = tableClient.GetTableRefer- vice&comp=stats
ence(" "); Method POST
table.CreateIfNotExists(); Request Headers https://myaccount.table.cosmosdb.azure.com/mytable
- Authorization - Date or x-ms-date
Request Headers
Create table entity - x-ms-version - x-ms-client-request-id
- Authorization - Date or x-ms-date
CustomerEntity item = new CustomerEntity() - x-ms-version - Content-Type
{ Query table
- Content-Length - Accept
PartitionKey = Guid.NewGuid().ToString(), Method GET - Prefer - x-ms-client-request-id
RowKey = Guid.NewGuid().ToString(),
Email = $" ", https://myaccount.table.cosmosdb.azure.com/Tables
PhoneNumber = "",
Bio = GetRandomString(1000) Request Headers Insert or Merge entity / Insert or Replace entity
}; - Authorization - Date or x-ms-date
Method MERGE / Method PUT
- x-ms-version - x-ms-client-request-id
https://myaccount.table.cosmosdb.azure.com/mytable(Partition-
Insert operation Key='myPartitionKey',RowKey='myRowKey')
Create table
TableOperation = TableOperation.Insert(item); Request Headers
table.Execute(insertOperation); Method POST
- Authorization - Date or x-ms-date
https://myaccount.table.cosmosdb.azure.com/Tables
- x-ms-version - Content-Type
Retrieve operation Request Headers - Content-Length - x-ms-client-request-id
© 2018 Microsoft Corporation. All rights reserved. www.cosmosdb.com | Tweet: @AzureCosmosDB, #cosmosdb
Azure Cosmos DB Query Cheat Sheet
Gremlin / Graph API
Anatomy of Tinkerpop Graph Select a vertex by property Count number of vertices
Start with the g.v() selector, and filter using the Use the .count() step at the end of a traversal.
.has ("key", "value") with any exisiting property
Vertex
as the key. Example:
id: 3
<- Input
label: “ingredient”
name: “cheese” Example: :> g.V().out().hasLabel(“ingredient")
Output ->
Edge id: 5
id: 4 label: “has” After selecting a vertex, use the .addE("label") step,
label: “has” Vertex followed by any number of properties, then use the Return the path of a traversal
id: 1 .to(g.V() …) to select another vertex to point it to.
Use the .path() step at the end of a traversal,
label: “food” use the .by() step to return values for a
name: “sandwich” Example:
specific field.
Vertex isDelicious: true :> g.V().has("name", "sandwich")
id: 2 .addE("has")
label: “ingredient”
Example:
.property("amount", 2)
name: “bread” .to(g.V().has("name", "bread")) <- Input
:> g.V().out()
.property("amount", 1)
Vertices (also known as nodes): Represent objects. They have .to(g.V().has("name", “cheese")) Output ->
unique ID, a unique label and one ore more properties. :> ["sandwich", "bread"], ["sandwich", “cheese"]
Edges: Represent relationships between objects (vertices). They
have unique Id, a unique label and one or more properties. Traverse a graph
After selecting a vertex, use the .out("label") step Select a specific element from a traversal
Graph object attributes to select all adjacent vertices that are connected with
Use the .as() step to name an object, and the
edges that have a specific label.
.select() step to return it at the end of a
ID: A property that uniquely identifies each element (vertex or
traversal. After that, you can use the .by() step to
edge) of a graph. Example:
get their values.
Labels: A unique string element that can be used to identify <- Input
types of objects (vertices or edges). This is a required field for :> g.V().has("name", "sandwich")
any grapph object. .out("has") Example:
Property: A key-value pair that can contain strings, boolean or .properties("name") <- Input
number values. :> g.V().outE().as("e").inV()
Output -> .select("e")
:> [cheese, bread] .by("amount")
Gremlin language primer
Output ->
Try this queries with the Gremlin console or the Data
:> [2, 1]
Explorer on Azure Portal. Loop a traversal
© 2018 Microsoft Corporation. All rights reserved. www.cosmosdb.com | Tweet: @AzureCosmosDB, #cosmosdb