0% found this document useful (0 votes)
44 views

Microsoft Documentdb SQL Query Cheat Sheet v4

This cheat sheet provides examples of common SQL queries that can be used to retrieve data from JSON documents in DocumentDB. It includes sample queries using operators like comparison, logical, ORDER BY, IN, ternary, coalesce, and examples combining SQL and JSON as well as intra-document joins.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Microsoft Documentdb SQL Query Cheat Sheet v4

This cheat sheet provides examples of common SQL queries that can be used to retrieve data from JSON documents in DocumentDB. It includes sample queries using operators like comparison, logical, ORDER BY, IN, ternary, coalesce, and examples combining SQL and JSON as well as intra-document joins.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Microsoft Azure DocumentDB Query Cheat Sheet

This cheat sheet helps you quickly write DocumentDB queries by showing some common SQL queries used to
retrieve data from two simple JSON documents.

Example Family JSON Documents SQL Query Sample Queries

1
{ -- Find families by ID
Comparison SELECT *
"id":"AndersenFamily", SELECT *
"lastName":"Andersen", FROM Families f (range) FROM Families.children[0] c
operators WHERE c.grade >= 5
"parents":[ WHERE f.id = "AndersenFamily"
{
"firstName":"Thomas"
}, [{ SELECT *
{ "id": "AndersenFamily", Logical FROM Families.children[0] c
"firstName":"Mary Kay" "lastName": "Andersen", operators WHERE c.grade >= 5 AND c.isRegistered =
} … true
], }]
"children":[
{
ORDER BY SELECT f.id, f.address.city
"firstName":"Henriette Thaulow",
SQL + JSON FROM Families f
"gender":"female", keyword ORDER BY f.address.city
"grade":5, -- Find families where City equals State and return
"pets":[ Name and City
{
"givenName":"Fluffy"
} SELECT {"Name":f.id, "City":f.address.city} AS Family SELECT *
FROM Families f FROM Families
] IN keyword
} WHERE f.address.city = f.address.state WHERE Families.address.state IN ("NY",
], "WA", "CA", "PA", "OH", "OR", "MI", "WI")
"address":{ [{
"state":"WA", "Family": {
"county":"King",
"city":"seattle"
"Name": "WakefieldFamily", Ternary (?) SELECT (c.grade < 5)? "elementary":
"City": "NY" and ((c.grade < 9)? "junior": "high") AS
},
}
"creationDate":"2015-01-03T12:00Z", Coalesce (??) gradeLevel
}]
"isRegistered":true, operators FROM Families.children[0] c
"location":{
"type":"Point",
"coordinates":[
31.9, SQL + intra JOIN SELECT f["lastName"]
-4.8 Escape/quoted FROM Families f
] -- Get the child names using an intra-document JOIN accessor WHERE f["id"] = "AndersenFamily"
}
} SELECT c.givenName
FROM Families f
{ SELECT [f.address.city, f.address.state]
2
JOIN c IN f.children Object/Array
"id":"WakefieldFamily", AS CityState
WHERE f.id = 'WakefieldFamily'
"parents":[ Creation FROM Families f
ORDER BY f.address.city ASC
{
"familyName":"Wakefield",
"givenName":"Robin"
[
},
{ { "givenName": "Jesse" }, Value SELECT VALUE "Hello World"
"familyName":"Miller", { "givenName": "Lisa"} keyword
"givenName":"Ben" ]
}
], SELECT
"children":[
{
SQL + JavaScript UDF f.id AS familyName,
c.givenName AS childGivenName,
"familyName":"Merriam", Intra-
-- Register UDF for REGEX_MATCH with this code c.firstName AS childFirstName,
"givenName":"Jesse", document p.givenName AS petName
"gender":"female",
"grade":1, function (input, pattern) { JOINS FROM Families f
"pets":[ return input.match(pattern) !== null; JOIN c IN f.children
{ } JOIN p IN c.pets
"givenName":"Goofy"
}, -- Use JavaScript
{ SELECT udf.REGEX_MATCH(Families.address.city, ".*eattle") SELECT *
"givenName":"Shadow"
}
Parameterized FROM Families f
[ SQL WHERE f.lastName = @lastName AND
] f.address.state = @addressState
{
},
{ "$1": true
"familyName":"Miller", },
"givenName":"Lisa", { SELECT Families.id, Families.address.city
"gender":"female", "$1": false String Built-in FROM Families
"grade":8 } functions WHERE STARTSWITH(Families.id,
} ] "Wakefield")
],
"address":{
"state":"NY", SELECT Families.id
"county":"Manhattan", Operators Array Built-in FROM Families
"city":"NY" WHERE ARRAY_CONTAINS(Families.parents, {
}, functions givenName: "Robin", familyName:
Arithmetic +, -, *, /, %
"creationDate":"2015-07-20T12:00Z", "Wakefield" })
"isRegistered":false
} |, &, ^, <,>>, >>> (zero-fill right
Bitwise
shift) Math Built-in SELECT VALUE ABS(-4)
Built-in functions functions

ABS, CEILING, EXP, FLOOR, LOG, Logical AND, OR, NOT


SELECT IS_DEFINED(f.lastName), IS_NUM-
LOG10, POWER, ROUND, SIGN, SQRT, Type Built-in
BER(4)
Mathematical SQUARE, TRUNC, ACOS, ASIN, ATAN, functions
ATN2, COS, COT, DEGREES, PI, Comparison =, !=, >, >=, <, <=, <>, ?? FROM Families f
RADIANS, SIN, and TAN
SELECT *
IS_ARRAY, IS_BOOL, IS_NULL, BETWEEN FROM Families.children[0] c
Type checking IS_NUMBER, IS_OBJECT, IS_STRING, String || (concatenate)
keyword WHERE c.grade BETWEEN 1 AND 5
IS_DEFINED, and IS_PRIMITIVE

CONCAT, CONTAINS, ENDSWITH, INDEX_OF, Ternary ?

String
LEFT, LENGTH, LOWER, LTRIM, REPLACE, TOP SELECT TOP 100 *
REPLICATE, REVERSE, RIGHT, RTRIM, keyword FROM Families f
STARTSWITH, SUBSTRING, and UPPER Query Interfaces
Array ARRAY_CONCAT, ARRAY_CONTAINS,
ARRAY_LENGTH, and ARRAY_SLICE
Server-side SQL, JavaScript integrated query SELECT *
Geospatial FROM Families f
functions WHERE ST_Distance(f.location,
ST_WITHIN, ST_DISTANCE, ST_INTERSECTS, Client-side .NET (LINQ), Java, JavaScript, Node.js, Python {"type":"Point", "coordinates":[31.9,
Geospatial ST_ISVALID, and ST_ISVALIDDETAILED -4.8]}) < 30000

www.documentdb.com | Online Query Playground: www.documentdb.com/sql/demo | RU Estimator: www.documentdb.com/capacityplanner


Tweet: @documentdb | StackOverflow: #azure-documentdb

You might also like