Getting user info using JOINs in Cosmos DB - azure-cosmosdb

The following is my Company object that I store in Cosmos DB. I have all the essential information about employees in Employees property. I also have a Departments property that both defines departments as well as its members.
{
"id": "company-a",
"name": "Company A",
"employees": [
{
"id": "john-smith",
"name": "John Smith",
"email": "john.smith#example.com"
},
{
"id": "jane-doe",
"name": "Jane Doe",
"email": "jane.doe#example.com"
},
{
"id": "brian-green",
"name": "Brian Green",
"email": "brian.green#example.com"
}
],
"departments": [
{
"id": "it",
"name": "IT Department",
"members": [
{
"id": "john-smith",
"name": "John Smith",
"isDepartmentHead": true
},
{
"id": "brian-green",
"name": "Brian Green",
"isDepartmentHead": false
}
]
},
{
"id": "hr",
"name": "HR Department",
"members": [
{
"id": "jane-doe",
"name": "Jane Doe",
"isDepartmentHead": true
}
]
}
]
}
I'm trying to return a list of a particular department, including the employee's email which will come from employees property.
Here's what I did but this is including all employees in the output:
SELECT dm.id, dm.name, e.email, em.isDepartmentHead
FROM Companies c
JOIN d IN c.departments
JOIN dm IN d.members
JOIN e IN c.Employees
WHERE c.id = "company-a" AND d.id = "hr"
The correct output would be:
[
{
"id": "jane-doe",
"name": "Jane Doe",
"email": "jane.doe#example.com",
"isDepartmentHead": true
}
]
How do I form my SQL statement to get all members of a department AND include employees' email addresses?

I'm pretty sure you cannot write a query like this. You are trying to correlate data twice in the same query across two arrays which I don't think is possible. (at least I've never been successful doing this).
Even if this was possible though, there are other issues with your data model. This data model will not scale. You also need to avoid unbounded or very large arrays within documents (e.g. employees and departments). You also do not want to store unrelated data in the same document. Objective here is to model data for high concurrency operations in the way you use it. This reduces both latency and cost.
There are many ways in which you can remodel this data. But if this is a very small data set, you could probably do something like this below with a partition key of companyId (assuming that you always query within a single company). This will store all employees for one company in the same logical partition which can store up to 20GB of data. I would also model this such that one document stores data specific to the company itself (address, phone number, number of employess, etc) with the id and companyId having the same value. This lets you do things like store materialized aggregates like # of employees and update it in a transaction. Also, since this approach mixes different types of entities (a bonus for NoSQL database, you need a discriminator property that allows you to filter for specific entities within the container so you can deserialize them directly into your model classes.
Here is a data model you could try (please note, you need to determine if this works for you by scaling it up to the amount of data you believe you will need to store. You also need to test and measure the RU/s cost for the CRUD operations you will execute with high concurrency).
Example company document:
{
"id": "aaaaa",
"companyId": "aaaaa",
"companyName": "Company A",
"type": "company",
"numberOfEmployees: 3,
"addresses": [
{
"address1": "123 Main Street",
"address2": "",
"city": "Los Angeles",
"state": "California",
"zip": "92568"
}
]
}
Then an employee document like this:
{
"id": "jane-doe",
"companyId": "aaaaa",
"type": "employee",
"employeeId": "jane-doe",
"employeeName": "Jane Doe",
"employeeEmail": "jane.doe#example.com",
"departmentId": "hr",
"departmentName": "HR Department",
"isDepartmentHead": true
}
Then last, here's the query to get the data you need.
SELECT
c.employeeId,
c.employeeName,
c.employeeEmail,
c.IsDepartmentHead
FROM c
WHERE
c.companyId = "company-a" AND
c.type = "employee" AND
c.departmentId = "hr"

Related

CosmosDb querying in sub-object

I'm trying to find accounts with a "extra-storage" premium package for a particular user in my Azure CosmosDb database. Here's what my Account object looks like:
{
"id": 123,
"name": "My Account",
"members": [
{
"id": 333,
"name": "John Smith"
},
{
"id": 555,
"name": "Jane Doe"
}
],
"subscription": {
"type": "great-value",
"startDate": "2022-04-21T16:38:00.0000000Z",
"premiumPackages": [
{
"type": "extra-storage",
"status": "active"
},
{
"type": "video-encoding",
"status": "cancelled"
}
]
}
}
So, my conditions for the query (in-English) are:
Account must contain "John Smith" (id: 333) as a member
It must have the "extra-storage" premium package in its subscription
I'm not sure if I can have multiple JOINs but here's what I've tried with no results so far:
SELECT c.id, c.name, s.premiumPackages.status
FROM c JOIN m IN c.members
JOIN s IN c.subscription
WHERE CONTAINS(m.id, 333)
AND CONTAINS(s.premiumPackages.type, "extra-storage")
Any idea how I can get accounts with "extra-storage" package for "John Smith"?
This query should give you what you are looking for.
SELECT c.id, c.name, premiumPackages.status
FROM c
JOIN (SELECT VALUE m FROM m IN c.members WHERE m.id = 333)
JOIN (SELECT VALUE s FROM s IN c.subscription.premiumPackages WHERE s.type
= "extra-storage") AS premiumPackages
This blog post on Understanding how to query arrays in Azure Cosmos DB is helpful to keep bookmarked when trying to write queries for arrays.
PS, id on the root in Cosmos DB must be a string so your "id": 123 should be "id": "123".

Azure search service index pointing multiple document db collections

How to load data from two separate collections of azure cosmos db to a single azure search index? I need a solution to join the data from two collections in a way similar to inner joining concept of SQL and load that data to azure search service.
I have two collections in azure cosmos db.
One for product and sample documents for the same is as below.
{
"description": null,
"links": [],
"replaces": "00000000-0000-0000-0000-000000000000",
"replacedBy": "00000000-0000-0000-0000-000000000000",
"productTypeId": "ccd0bc73-c4a1-41bf-9c96-454a5ba1d025",
"id": "a4853bf5-9c58-4fb5-a1ff-fc3ab575b4c8",
"name": "New Product",
"createDate": "2018-09-19T10:04:35.1951552Z",
"createdBy": "00000000-0000-0000-0000-000000000000",
"updateDate": "2018-10-05T13:46:24.7048358Z",
"updatedBy": "DIJdyXMudaqeAdsw1SiNyJKRIi7Ktio5#clients"
}
{
"description": null,
"links": [],
"replaces": "00000000-0000-0000-0000-000000000000",
"replacedBy": "00000000-0000-0000-0000-000000000000",
"productTypeId": "ccd0bc73-c4a1-41bf-9c96-454a5ba1d025",
"id": "b9b6c3bc-a8f8-470f-ac93-be589eb1da16",
"name": "New Product 2",
"createDate": "2018-09-19T11:02:02.6919008Z",
"createdBy": "00000000-0000-0000-0000-000000000000",
"updateDate": "2018-09-19T11:02:02.6919008Z",
"updatedBy": "00000000-0000-0000-0000-000000000000"
}
{
"description": null,
"links": [],
"replaces": "00000000-0000-0000-0000-000000000000",
"replacedBy": "00000000-0000-0000-0000-000000000000",
"productTypeId": "ccd0bc73-c4a1-41bf-9c96-454a5ba1d025",
"id": "98b3647a-3b40-4a00-bd0f-2a397bd48b68",
"name": "New Product 7",
"createDate": "2018-09-20T09:42:28.2913567Z",
"createdBy": "00000000-0000-0000-0000-000000000000",
"updateDate": "2018-09-20T09:42:28.2913567Z",
"updatedBy": "00000000-0000-0000-0000-000000000000"
}
Another collection for ProductType with below sample document.
{
"description": null,
"links": null,
"replaces": "00000000-0000-0000-0000-000000000000",
"replacedBy": "00000000-0000-0000-0000-000000000000",
"id": "ccd0bc73-c4a1-41bf-9c96-454a5ba1d025",
"name": "ProductType1_186",
"createDate": "2018-09-18T23:54:43.9395245Z",
"createdBy": "00000000-0000-0000-0000-000000000000",
"updateDate": "2018-10-05T13:29:44.019851Z",
"updatedBy": "DIJdyXMudaqeAdsw1SiNyJKRIi7Ktio5#clients"
}
The product type id is referred in product collection and that is the column which links both the collections.
I want to load the above two collections to the same azure search service index and I expect my field of index to be populated somewhat like below.
If you use product id as the key, you can simply point two indexers at the same index, and Azure Search will merge the documents automatically. For example, here are two indexer definitions that would merge their data into the same index:
{
"name" : "productIndexer",
"dataSourceName" : "productDataSource",
"targetIndexName" : "combinedIndex",
"schedule" : { "interval" : "PT2H" }
}
{
"name" : "sampleIndexer",
"dataSourceName" : "sampleDataSource",
"targetIndexName" : "combinedIndex",
"schedule" : { "interval" : "PT2H" }
}
Learn more about the create indexer api here
However, it appears that the two collections share the same fields. This means that the fields from the document which gets indexed last will replace the fields from the document that got indexed first. To avoid this, I would recommend replacing the fields that match the 00000000-0000-0000-0000-000000000000 pattern with null in your Cosmos DB query. For example:
SELECT productTypeId, (createdBy != "00000000-0000-0000-0000-000000000000" ? createdBy : null) as createdBy FROM products
This exact query may not work for your use case. See the query syntax reference for more information.
Please let me know if you have any questions, or something is not working as expected.
Thanks
Matt

Document Db query filter for an attribute that contains an array

With the sample json shown below, am trying to retrieve all documents that contains atleast one category which is array object wrapped underneath Categories that has the text value 'drinks' with the following query but the returned result is empty. Can someone help me get this right?
SELECT items.id
,items.description
,items.Categories
FROM items
WHERE ARRAY_CONTAINS(items.Categories.Category.Text, "drink")
{
"id": "1dbaf1d0-6549-11a0-88a8-001256957023",
"Categories": {
"Category": [{
"Type": "GS1",
"Id": "10000266",
"Text": "Stimulants/Energy Drinks Ready to Drink"
}, {
"Type": "GS2",
"Id": "10000266",
"Text": "Healthy Drink"
}]
}
},
Note: The json is a bit wierd to have the array wrapped by an object itself - this json was converted from a XML hence the result. So please assume I do not have any control over how this object is saved as json
You need to flatten the document in your query to get the result you want by joining the array back to the main document. The query you want would look like this:
SELECT items.id, items.Categories
FROM items
JOIN Category IN items.Categories.Category
WHERE CONTAINS(LOWER(Category.Text), "drink")
However, because there is no concept of a DISTINCT query, this will produce duplicates equal to the number of Category items that contain the word "drink". So this query would produce your example document twice like this:
[
{
"id": "1dbaf1d0-6549-11a0-88a8-001256957023",
"Categories": {
"Category": [
{
"Type": "GS1",
"Id": "10000266",
"Text": "Stimulants/Energy Drinks Ready to Drink"
},
{
"Type": "GS2",
"Id": "10000266",
"Text": "Healthy Drink"
}
]
}
},
{
"id": "1dbaf1d0-6549-11a0-88a8-001256957023",
"Categories": {
"Category": [
{
"Type": "GS1",
"Id": "10000266",
"Text": "Stimulants/Energy Drinks Ready to Drink"
},
{
"Type": "GS2",
"Id": "10000266",
"Text": "Healthy Drink"
}
]
}
}
]
This could be problematic and expensive if the Categories array holds a lot of Category items that have "drink" in them.
You can cut that down if you are only interested in a single Category by changing the query to:
SELECT items.id, Category
FROM items
JOIN Category IN items.Categories.Category
WHERE CONTAINS(LOWER(Category.Text), "drink")
Which would produce a more concise result with only the id field repeated with each matching Category item showing up once:
[{
"id": "1dbaf1d0-6549-11a0-88a8-001256957023",
"Category": {
"Type": "GS1",
"Id": "10000266",
"Text": "Stimulants/Energy Drinks Ready to Drink"
}
},
{
"id": "1dbaf1d0-6549-11a0-88a8-001256957023",
"Category": {
"Type": "GS2",
"Id": "10000266",
"Text": "Healthy Drink"
}
}]
Otherwise, you will have to filter the results when you get them back from the query to remove duplicate documents.
If it were me and I was building a production system with this requirement, I'd use Azure Search. Here is some info on hooking it up to DocumentDB.
If you don't want to do that and we must live with the constraint that you can't change the shape of the documents, the only way I can think to do this is to use a User Defined Function (UDF) like this:
function GetItemsWithMatchingCategories(categories, matchingString) {
if (Array.isArray(categories) && categories !== null) {
var lowerMatchingString = matchingString.toLowerCase();
for (var index = 0; index < categories.length; index++) {
var category = categories[index];
var categoryName = category.Text.toLowerCase();
if (categoryName.indexOf(lowerMatchingString) >= 0) {
return true;
}
}
}
}
Note, the code above was modified by the asker after actually trying it out so it's somewhat tested.
You would use it with a query like this:
SELECT * FROM items WHERE udf.GetItemsWithMatchingCategories(items.Categories, "drink")
Also, note that this will result in a full table scan (unless you can combine it with other criteria that can use an index) which may or may not meet your performance/RU limit constraints.

WHERE clause on an array in Azure DocumentDb

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 ?
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.

What would the freebase MQL query for this?

I want to find the ceo of IBM. What would be the MQL query for this?
The MQL for this search looks like the following.
This particular instance may be a tat more complicated than necessary because I got it initially produced from a Freebase interactive search and then simply added/improved the filters manually.
I verified it with various company names with relative success, i.e. it works provided that the underlying data is properly codified in Freebase (some companies are missing, for some companies the leadership data is incomplete etc.)
There are a few tricks to this query:
the company name in u0 fitler needs to match precisely the company name as recorded in Freebase. You could use a contains predicate rather than an equal one, but that could introduce many irrelevant hits. For example you need to use "IBM", "Apple Inc.", "General Motors" rather than common alternatives to these names ("International Business Machines", "Apple", "GM"...")
the u1 filter, on the leadership role is expressed in a extensive One of predicate because unfortunately the nomenclature for these roles is relatively loose, with duplicates (e.g. could be CEO or Chief Executive Officer) and with the fact that the role of CEO is often coupled with other corporate roles such as Chairman [of the board] and/or President etc. I hand picked this list by first looking up (in Freebase) the instances of Leadership Roles which contained "CEO" or "Chief Executive".
the u2 filter expresses that the to date should be empty, to select only the person currently in office, as opposed to former CEOs (for which hopefully Freebase recorded the end date of their tenure).
Depending on your application, you may need to test that the query returns one and exactly one record, and adapt accordingly if it doesn't.
Freebase MQL editor is a convenient tool test and edit with this kind of queries.
[
{
"from": null,
"id": null,
"limit": 20,
"organization": {
"id": null,
"name": null,
"optional": true
},
"person": {
"id": null,
"name": null,
"optional": true
},
"role": {
"id": null,
"name": null,
"optional": true
},
"s0:type": [
{
"id": "/organization/leadership",
"link": [
{
"timestamp": [
{
"optional": true,
"type": "/type/datetime",
"value": null
}
],
"type": "/type/link"
}
],
"type": "/type/type"
}
],
"sort": "s0:type.link.timestamp.value",
"title": null,
"to": null,
"type": "/organization/leadership",
"u0:organization": [
{
"id": null,
"name": "IBM",
"type": "/organization/organization"
}
],
"u1:role": [
{
"id": null,
"name|=": ["Chief Executive Officer", "President and CEO", "Chairman and CEO", "Interim CEO", "Interim Chief Executive Officer", "Founder and CEO", "Chairman, President and CEO", "Managing Director and CEO", "Executive Vice President and Chief Operating Officer", "Co-Founder, Chairman and Chief Executive Officer"],
"type": "/organization/role"
}
],
"u2:to": [
{
"value": null,
"optional": "forbidden"
}
]
}
]​
Sample return (for "IBM", specifically)
{
"code": "/api/status/ok",
"result": [{
"from": "2012-01-01",
"id": "/m/09t7b08",
"organization": {
"id": "/en/ibm",
"name": "IBM"
},
"person": {
"id": "/en/virginia_m_rometty",
"name": "Virginia M. Rometty"
},
"role": {
"id": "/en/chairman_president_and_ceo",
"name": "Chairman, President and CEO"
},
"s0:type": [{
"id": "/organization/leadership",
"link": [{
"timestamp": [{
"type": "/type/datetime",
"value": "2010-01-23T08:02:57.0006Z"
}],
"type": "/type/link"
}],
"type": "/type/type"
}],
"title": "Chairman, President and CEO",
"to": null,
"type": "/organization/leadership",
"u0:organization": [{
"id": "/en/ibm",
"name": "IBM",
"type": "/organization/organization"
}],
"u1:role": [{
"id": "/en/chairman_president_and_ceo",
"type": "/organization/role"
}],
"u2:to": []
}

Resources