Give the following documents stored in Cosmos DB, how do I go about getting all of the Child/Children elements where the FirstName field of each child is "Bob"? I'm trying to use the SQL query syntax, but have not found the right way to do this that combines both document schema results.
// Document 1
{
"id": "document1",
"Child": {
"FirstName": "Bob",
"LastName": "Smith"
}
}
// Document 2
{
"id": "document2",
"Children": [
{
"Name": "Bob",
"LastName": "Jones"
},
{
"Name": "Sue",
"LastName": "Jones"
}
]
}
I'm trying to write a query that looks for all "Bob" child elements to achieve the following output:
[
{
"FirstName": "Bob",
"LastName": "Smith"
},
{
"Name": "Bob",
"LastName": "Jones"
},
]
Cosmos db documents are stored as json format, you can't treat the Child property(Superior structure) and Children property(Sub structure) equally with a single sql query.
Then can't be flatten and put into one object,please see the example:
The c.Child does not display. So,i'm afraid you need to query Child and Children separately, then merge them for your requirements.
I tried to explain here. In one single query sql is not possible. For example, document 1 does not includes Children Array,document 2 does. In one single sql, C JOIN Children is necessary. But for document 1, Child join nothing is nothing so that no results will be pulled out. You could try it.
Since UNION feature is not supported by cosmos db, i still suggest following above suggestion to query them separately and merge.
Related
For some time when I had to find particular element of array by particular value I've been using ARRAY_CONTAINS function. Now I have documents with nested arrays where I have to search not but particular value, but using regex.
As an example of document let me use one from official documentation:
{
"id": "AndersenFamily",
"lastName": "Andersen",
"parents": [
{ "firstName": "Thomas" },
{ "firstName": "Mary Kay"}
],
"children": [
{
"firstName": "Henriette Thaulow",
"gender": "female",
"grade": 5,
"pets": [{ "givenName": "Fluffy" }]
}
],
"address": { "state": "WA", "county": "King", "city": "Seattle" },
"creationDate": 1431620472,
"isRegistered": true
}
What I need is to select and fully get all documents where at least one of children has at least one pets element where givenName contains "fluf".
What SQL query do I build to achieve it?
Here's a query that uses JOINs to flatten out the inner pets arrays and apply a filter, then return the entire matching family items:
SELECT VALUE f
FROM Families f
JOIN c IN f.children
JOIN p IN c.pets
WHERE p.givenName LIKE "%Fluf%"
The complexity of figuring out such queries is one reason why I think it's worth considering modeling data to be as flat as possible, including normalizing out to have separate pets items for example, which can be queried with direct property filters without dealing with nesting. Combining everything into a large Family object as the examples do isn't necessarily a good idea in practice depending on your goals.
We are experiencing an issue in when writing queries for Cosmos Document DB and we want to create a new document property and use it in an ORDER BY clause
If, for example, we had a set of documents like:
{
"Name": "Geoff",
"Company": "Acme"
},
{
"Name": "Bob",
"Company": "Bob Inc"
}
...and we write a query like SELECT * FROM c ORDER BY c.Name this works fine and returns both documents
However, if we were to add a new document with an additional property:
{
"Name": "Geoff",
"Company": "Acme"
},
{
"Name": "Bob",
"Company": "Bob Inc"
},
{
"Name": "Sarah",
"Company": "My Company Ltd",
"Title": "President"
}
...and we write a query like SELECT * FROM c ORDER BY c.Title it will only return the document for Sarah and excludes the 2 without a Title property.
This means that the ORDER BY clause is behaving like a filter rather than just a sort, which seems unexpected.
It seems that all document schemas are likely to add properties over time. Unless we go back and add these properties to all existing document records in the container then we can never use them in an ORDER BY clause without excluding records.
Does anyone have a solution to allow the ORDER BY to only effect the Sort order of the result set?
Currently, ORDER BY works off of indexed properties, and missing values are not included in the result of a query using ORDER BY.
As a workaround, you could do two queries and combine the results:
The current query you're doing, with ORDER BY, returning all documents containing the Title property, ordered
A second query, returning all documents that don't have Title defined.
The second query would look something like:
SELECT * FROM c
WHERE NOT IS_DEFINED(c.Title)
Also note that, according to this note within the EF Core repo issue list, behavior is a bit different when using compound indexes (where documents with missing properties are returned).
How can I have this structure in a query string?
"properties": {
"list": [
{
"label": "bye",
"value": "world"
},
{
"label": "hello",
"value": "mars"
}
]
}
I've tried it with properties[][list][label]=bye&properties[][list][value]=world&properties[0][label]=hello&properties[0][value]=mars and also with properties[][list][label]=bye&properties[][list][value]=world&properties[][list][label]=hello&properties[][list][value]=mars, none of them worked. I built them in php with http_build_query.
I need to have this structure in a query string because I have to send the data along with some other stuff with POST to a PHP site.
I see two errors in your query string:
properties is an object, so there's no need to use [] to add elements.
list is an array, so you must use numeric indexes in the query string.
The correct query string is:
?properties[list][0][label]=bye
&properties[list][0][value]=world
&properties[list][1][label]=hello
&properties[list][1][value]=mars
(multi-lined for readability)
I'd like to query Freebase in order to obtain all the relations that occur between two concepts.
My problem is that I'd like to do this using two concepts' names.
For example: I have "Ball" as a concept (and I don't want to say to Freebase if it is Ball as in Dance or as in Sports Equipment) and I want to find out if there are relations between it and "Football" (in this case, there is just one concept on Freebase for Football, so it is easier); the ouput should be "/sports/sports_equipment/sport_used_for".
For now I just managed to do the same query using the id of the concepts, but I would like to search for relations as I explained before, without knowing the exact meaning of the concept title (Ball in the example).
The query that I have so far is this:
{
"type": "/type/link",
"source": {
"id": "/m/0dpm1v"
},
"master_property": null,
"target": {
"id": "/m/02vx4"
}
}
Thank you in advance for the help
"name" was what I was looking for. I don't know because I did not try/find this before.
Thak you anyway.
{
"type": "/type/link",
"source": {
"name": "ball"
},
"master_property": null,
"target": {
"name": "football"
}
}
Let's say I want to get all movies in which at least two (different) actors called "John" played:
Example query:
[{
"type":"/film/film",
"name":null,
"limit":10,
"/film/film/initial_release_date":"2005"
"starring":[{
"a:actor": [{
"type": "/film/actor",
"name": null,
"name~=": "John",
}],
"b:actor": [{
"type": "/film/actor",
"name": null,
"name~=": "John",
}]
}]
}]
If you run the example query, you will see that it will list movies with only one "John" in them. How can I fix my query to exclude these results with duplicated children?
In general, you'll have to do the filtering client-side; queries in MQL are "tree-like" in that one part of the query can't refer to another part rather than being a generic graph.
In this case, you could look for films which have more than one "John" acting in them; however, MQL doesn't allow you to filter on a derived property like "count", so the best you can do is to reverse sort based on the count and then just stop processing as soon as you hit the first entry with "count": 1. However, that query times out if you remove the fixed 1935 release date (sorting in MQL kills performance), so you're probably stuck with just simple client-side filtering.