Querying nested subtrees in Cosmos DB
Let's say I have a parent-child-grandchild-etc relationship in a Cosmos document, represented by the following JSON:
"id": "someUniqueString",
"peepsNkids": [
"Jane": [
"Joe": ,
"Jocelyn": [
"Jerry": ,
"Jan": [
"Tom": ,
"Dick": ,
"Harry":
],
"Jim":
],
"Mary": [
"Moe": ,
"Larry": ,
"Dorothy": [
"Eadie": ,
"Phil": ,
"Lucille": [
"Desi Jr":
]
]
]
]
How can I query cleanly (in Storage Explorer and C#) for a subtree that, using the syntax of the "Sub-documents" section of this post titled "Query DocumentDB", would resolve to:
SELECT * FROM peepsNkids.Jane.Jocelyn.Jan
... with the expectation that the following would be returned?
[
"Tom": ,
"Dick": ,
"Harry":
]
I don't think I need to worry about the original id here. Worst case is that I get back multiple records that have Jane.Jocelyn.Jan
, and since in my "real" setup the "names" are all unique ids, getting back multiple rows would indicate a serious schematic issue.
I can obviously create a potentially cyclical object model with string name
and List<PersonAndKids> peepsNkids
, then get back the full entry for the id "someUniqueString"
and then traverse the objects with lots of peepsNkids.TryGetValue("Jane", out firstParent)
kinds of stuff, but I'm looking for a way to do that in Cosmos rather than in my service's memory.
c# azure-cosmosdb document-database
add a comment |
Let's say I have a parent-child-grandchild-etc relationship in a Cosmos document, represented by the following JSON:
"id": "someUniqueString",
"peepsNkids": [
"Jane": [
"Joe": ,
"Jocelyn": [
"Jerry": ,
"Jan": [
"Tom": ,
"Dick": ,
"Harry":
],
"Jim":
],
"Mary": [
"Moe": ,
"Larry": ,
"Dorothy": [
"Eadie": ,
"Phil": ,
"Lucille": [
"Desi Jr":
]
]
]
]
How can I query cleanly (in Storage Explorer and C#) for a subtree that, using the syntax of the "Sub-documents" section of this post titled "Query DocumentDB", would resolve to:
SELECT * FROM peepsNkids.Jane.Jocelyn.Jan
... with the expectation that the following would be returned?
[
"Tom": ,
"Dick": ,
"Harry":
]
I don't think I need to worry about the original id here. Worst case is that I get back multiple records that have Jane.Jocelyn.Jan
, and since in my "real" setup the "names" are all unique ids, getting back multiple rows would indicate a serious schematic issue.
I can obviously create a potentially cyclical object model with string name
and List<PersonAndKids> peepsNkids
, then get back the full entry for the id "someUniqueString"
and then traverse the objects with lots of peepsNkids.TryGetValue("Jane", out firstParent)
kinds of stuff, but I'm looking for a way to do that in Cosmos rather than in my service's memory.
c# azure-cosmosdb document-database
If you figured out the problem, please post it as a proper answer, and not as an edit to your question. That way, this question and answer can be properly voted on, accordingly.
– David Makogon
Nov 20 '18 at 20:04
@DavidMakogon That's the plan. For some reason, I thought I had to wait two days to answer my own question; thanks for the update.
– ruffin
Nov 20 '18 at 20:25
@DavidMakogon Worth pointing out that I still am unable to use a query in the format shown at the link I provided, which remains an unanswered part of this question.
– ruffin
Nov 20 '18 at 20:33
add a comment |
Let's say I have a parent-child-grandchild-etc relationship in a Cosmos document, represented by the following JSON:
"id": "someUniqueString",
"peepsNkids": [
"Jane": [
"Joe": ,
"Jocelyn": [
"Jerry": ,
"Jan": [
"Tom": ,
"Dick": ,
"Harry":
],
"Jim":
],
"Mary": [
"Moe": ,
"Larry": ,
"Dorothy": [
"Eadie": ,
"Phil": ,
"Lucille": [
"Desi Jr":
]
]
]
]
How can I query cleanly (in Storage Explorer and C#) for a subtree that, using the syntax of the "Sub-documents" section of this post titled "Query DocumentDB", would resolve to:
SELECT * FROM peepsNkids.Jane.Jocelyn.Jan
... with the expectation that the following would be returned?
[
"Tom": ,
"Dick": ,
"Harry":
]
I don't think I need to worry about the original id here. Worst case is that I get back multiple records that have Jane.Jocelyn.Jan
, and since in my "real" setup the "names" are all unique ids, getting back multiple rows would indicate a serious schematic issue.
I can obviously create a potentially cyclical object model with string name
and List<PersonAndKids> peepsNkids
, then get back the full entry for the id "someUniqueString"
and then traverse the objects with lots of peepsNkids.TryGetValue("Jane", out firstParent)
kinds of stuff, but I'm looking for a way to do that in Cosmos rather than in my service's memory.
c# azure-cosmosdb document-database
Let's say I have a parent-child-grandchild-etc relationship in a Cosmos document, represented by the following JSON:
"id": "someUniqueString",
"peepsNkids": [
"Jane": [
"Joe": ,
"Jocelyn": [
"Jerry": ,
"Jan": [
"Tom": ,
"Dick": ,
"Harry":
],
"Jim":
],
"Mary": [
"Moe": ,
"Larry": ,
"Dorothy": [
"Eadie": ,
"Phil": ,
"Lucille": [
"Desi Jr":
]
]
]
]
How can I query cleanly (in Storage Explorer and C#) for a subtree that, using the syntax of the "Sub-documents" section of this post titled "Query DocumentDB", would resolve to:
SELECT * FROM peepsNkids.Jane.Jocelyn.Jan
... with the expectation that the following would be returned?
[
"Tom": ,
"Dick": ,
"Harry":
]
I don't think I need to worry about the original id here. Worst case is that I get back multiple records that have Jane.Jocelyn.Jan
, and since in my "real" setup the "names" are all unique ids, getting back multiple rows would indicate a serious schematic issue.
I can obviously create a potentially cyclical object model with string name
and List<PersonAndKids> peepsNkids
, then get back the full entry for the id "someUniqueString"
and then traverse the objects with lots of peepsNkids.TryGetValue("Jane", out firstParent)
kinds of stuff, but I'm looking for a way to do that in Cosmos rather than in my service's memory.
c# azure-cosmosdb document-database
c# azure-cosmosdb document-database
edited Nov 20 '18 at 20:26
ruffin
asked Nov 19 '18 at 15:21
ruffinruffin
9,14235291
9,14235291
If you figured out the problem, please post it as a proper answer, and not as an edit to your question. That way, this question and answer can be properly voted on, accordingly.
– David Makogon
Nov 20 '18 at 20:04
@DavidMakogon That's the plan. For some reason, I thought I had to wait two days to answer my own question; thanks for the update.
– ruffin
Nov 20 '18 at 20:25
@DavidMakogon Worth pointing out that I still am unable to use a query in the format shown at the link I provided, which remains an unanswered part of this question.
– ruffin
Nov 20 '18 at 20:33
add a comment |
If you figured out the problem, please post it as a proper answer, and not as an edit to your question. That way, this question and answer can be properly voted on, accordingly.
– David Makogon
Nov 20 '18 at 20:04
@DavidMakogon That's the plan. For some reason, I thought I had to wait two days to answer my own question; thanks for the update.
– ruffin
Nov 20 '18 at 20:25
@DavidMakogon Worth pointing out that I still am unable to use a query in the format shown at the link I provided, which remains an unanswered part of this question.
– ruffin
Nov 20 '18 at 20:33
If you figured out the problem, please post it as a proper answer, and not as an edit to your question. That way, this question and answer can be properly voted on, accordingly.
– David Makogon
Nov 20 '18 at 20:04
If you figured out the problem, please post it as a proper answer, and not as an edit to your question. That way, this question and answer can be properly voted on, accordingly.
– David Makogon
Nov 20 '18 at 20:04
@DavidMakogon That's the plan. For some reason, I thought I had to wait two days to answer my own question; thanks for the update.
– ruffin
Nov 20 '18 at 20:25
@DavidMakogon That's the plan. For some reason, I thought I had to wait two days to answer my own question; thanks for the update.
– ruffin
Nov 20 '18 at 20:25
@DavidMakogon Worth pointing out that I still am unable to use a query in the format shown at the link I provided, which remains an unanswered part of this question.
– ruffin
Nov 20 '18 at 20:33
@DavidMakogon Worth pointing out that I still am unable to use a query in the format shown at the link I provided, which remains an unanswered part of this question.
– ruffin
Nov 20 '18 at 20:33
add a comment |
1 Answer
1
active
oldest
votes
Nested query does not work
Your mistake appears to be believing that you have to query a sort of "nested table" like you mention in your question with this...
SELECT * FROM c.peepsNkids.Jane.Jocelyn.Jan
Now you're right, that's exactly what's demonstrated as a nested query in the link you provided, which includes this query:
SELECT *
FROM Families.address.state
I don't see a way to execute that in Storage Explorer. It would be interesting to find out how to use this construction or if it's possible.
Composite field equivalent
It is, however, possible to do an equivalent query that selects a sort of "composite field" that equates to a table. Try this:
SELECT peepsNkids.Jane.Jocelyn.Jan FROM c
Or, better yet, to constrain that query to a specific document by id, try this:
SELECT peepsNkids.Jane.Jocelyn.Jan FROM c WHERE c.id = 'someUniqueString'
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377712%2fquerying-nested-subtrees-in-cosmos-db%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Nested query does not work
Your mistake appears to be believing that you have to query a sort of "nested table" like you mention in your question with this...
SELECT * FROM c.peepsNkids.Jane.Jocelyn.Jan
Now you're right, that's exactly what's demonstrated as a nested query in the link you provided, which includes this query:
SELECT *
FROM Families.address.state
I don't see a way to execute that in Storage Explorer. It would be interesting to find out how to use this construction or if it's possible.
Composite field equivalent
It is, however, possible to do an equivalent query that selects a sort of "composite field" that equates to a table. Try this:
SELECT peepsNkids.Jane.Jocelyn.Jan FROM c
Or, better yet, to constrain that query to a specific document by id, try this:
SELECT peepsNkids.Jane.Jocelyn.Jan FROM c WHERE c.id = 'someUniqueString'
add a comment |
Nested query does not work
Your mistake appears to be believing that you have to query a sort of "nested table" like you mention in your question with this...
SELECT * FROM c.peepsNkids.Jane.Jocelyn.Jan
Now you're right, that's exactly what's demonstrated as a nested query in the link you provided, which includes this query:
SELECT *
FROM Families.address.state
I don't see a way to execute that in Storage Explorer. It would be interesting to find out how to use this construction or if it's possible.
Composite field equivalent
It is, however, possible to do an equivalent query that selects a sort of "composite field" that equates to a table. Try this:
SELECT peepsNkids.Jane.Jocelyn.Jan FROM c
Or, better yet, to constrain that query to a specific document by id, try this:
SELECT peepsNkids.Jane.Jocelyn.Jan FROM c WHERE c.id = 'someUniqueString'
add a comment |
Nested query does not work
Your mistake appears to be believing that you have to query a sort of "nested table" like you mention in your question with this...
SELECT * FROM c.peepsNkids.Jane.Jocelyn.Jan
Now you're right, that's exactly what's demonstrated as a nested query in the link you provided, which includes this query:
SELECT *
FROM Families.address.state
I don't see a way to execute that in Storage Explorer. It would be interesting to find out how to use this construction or if it's possible.
Composite field equivalent
It is, however, possible to do an equivalent query that selects a sort of "composite field" that equates to a table. Try this:
SELECT peepsNkids.Jane.Jocelyn.Jan FROM c
Or, better yet, to constrain that query to a specific document by id, try this:
SELECT peepsNkids.Jane.Jocelyn.Jan FROM c WHERE c.id = 'someUniqueString'
Nested query does not work
Your mistake appears to be believing that you have to query a sort of "nested table" like you mention in your question with this...
SELECT * FROM c.peepsNkids.Jane.Jocelyn.Jan
Now you're right, that's exactly what's demonstrated as a nested query in the link you provided, which includes this query:
SELECT *
FROM Families.address.state
I don't see a way to execute that in Storage Explorer. It would be interesting to find out how to use this construction or if it's possible.
Composite field equivalent
It is, however, possible to do an equivalent query that selects a sort of "composite field" that equates to a table. Try this:
SELECT peepsNkids.Jane.Jocelyn.Jan FROM c
Or, better yet, to constrain that query to a specific document by id, try this:
SELECT peepsNkids.Jane.Jocelyn.Jan FROM c WHERE c.id = 'someUniqueString'
answered Nov 20 '18 at 20:32
ruffinruffin
9,14235291
9,14235291
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377712%2fquerying-nested-subtrees-in-cosmos-db%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you figured out the problem, please post it as a proper answer, and not as an edit to your question. That way, this question and answer can be properly voted on, accordingly.
– David Makogon
Nov 20 '18 at 20:04
@DavidMakogon That's the plan. For some reason, I thought I had to wait two days to answer my own question; thanks for the update.
– ruffin
Nov 20 '18 at 20:25
@DavidMakogon Worth pointing out that I still am unable to use a query in the format shown at the link I provided, which remains an unanswered part of this question.
– ruffin
Nov 20 '18 at 20:33