In an Azure Documentdb document like this
{ "id": "WakefieldFamily", "parents": [ { "familyName": "Wakefield", "givenName": "Robin" }, { "familyName": "Miller", "givenName": "Ben" } ], "children": [ { "familyName": "Merriam", "givenName": "Jesse", "gender": "female", "grade": 1, "pets": [ { "givenName": "Goofy" }, { "givenName": "Shadow" } ] }, { "familyName": "Miller", "givenName": "Lisa", "gender": "female", "grade": 8 } ], "address": { "state": "NY", "county": "Manhattan", "city": "NY" }, "isRegistered": false };
How do I query to get children whose pets given name is "Goofy" ?
Looks like the following syntax is invalid
Select * from root r WHERE r.children.pets.givenName="Goofy"
Instead I need to do
Select * from root r WHERE r.children[0].pets[0].givenName="Goofy"
which is not really searching through an array.
Any suggestion on how I should handle queries like these ?
Microsoft Azure DocumentDB is really easy to use and allows for rapid application development. Being able to store heterogeneous JSON documents within DocumentDB and query it using SQL like syntax and LINQ is really great which means that the developer do not have to learn anything new.
Add the endpoint url and the authorization key. DocumentDB is the latest storage option added to Microsoft Azure. It is a no-sql storage service that stores JSON documents natively and provides indexing capabilities along with other interesting features. This wiki shall introduce you to this new service. Enter A Database ID and hit Create!
DocumentDB is the latest storage option added to Microsoft Azure. It is a no-sql storage service that stores JSON documents natively and provides indexing capabilities along with other interesting features. This wiki shall introduce you to this new service. Enter A Database ID and hit Create! Please wait while the database is being created.
When using the IN keyword for iteration, you cannot filter or project any properties outside of the array. Instead, you should use JOINs. For additional examples, read our blog post on working with arrays in Azure Cosmos DB. Learn about SQL system function DateTimePart in Azure Cosmos DB.
You should take advantage of DocumentDB's JOIN
clause, which operates a bit differently than JOIN
in RDBMs (since DocumentDB deals w/ denormlaized data model of schema-free documents).
To put it simply, you can think of DocumentDB's JOIN
as self-joins which can be used to form cross-products between nested JSON objects.
In the context of querying children whose pets given name is "Goofy", you can try:
SELECT f.id AS familyName, c AS child, p.givenName AS petName FROM Families f JOIN c IN f.children JOIN p IN c.pets WHERE p.givenName = "Goofy"
Which returns:
[{ familyName: WakefieldFamily, child: { familyName: Merriam, givenName: Jesse, gender: female, grade: 1, pets: [{ givenName: Goofy }, { givenName: Shadow }] }, petName: Goofy }]
Reference: http://azure.microsoft.com/en-us/documentation/articles/documentdb-sql-query/
Edit:
You can also use the ARRAY_CONTAINS
function, which looks something like this:
SELECT food.id, food.description, food.tags FROM food WHERE food.id = "09052" or ARRAY_CONTAINS(food.tags.name, "blueberries")
I think the ARRAY_CONTAINS function has changed since this was answered in 2014. I had to use the following for it to work.
SELECT * FROM c WHERE ARRAY_CONTAINS(c.Samples, {"TimeBasis":"5MIN_AV", "Value":"5.105"},true)
Samples is my JSON array and it contains objects with many properties including the two above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With