DocumentDb SELECT with JOIN not returning anything - azure

My document in DocumentDb looks like this:
{
"id": 123,
"timers":
{
"projectTimer":
{
"id": 234,
"name": "My Project",
"startTime": "10:35 AM"
},
"taskTimer":
{
"id": 789,
"name": "My Task",
"startTime": "10:45 AM"
}
}
}
The key points here are:
"timers" is an object -- NOT an array
The sub-objects are also set i.e. "projectTimer" and "taskTimer"
If I set my SELECT statement to the following, it works by giving me both projectTimer and taskTimer sub-objects
SELECT c.timers
FROM Collection c
WHERE c.id = 123
But the following returns nothing. I don't understand why because it seems like a really simple JOIN:
SELECT t.projectTimer
FROM Collection c
JOIN t IN c.timers
WHERE c.id = 123
Any idea where I'm making a mistake?

The issue is that you're trying to do a JOIN on something that's not an array.
If, instead, you reworked your document slightly:
{
"id": "123",
"timers": [
{
"projectTimer": {
"id": 234,
"name": "My Project",
"startTime": "10:35 AM"
}
},
{
"taskTimer": {
"id": 789,
"name": "My Task",
"startTime": "10:45 AM"
}
}
],
}
You'd then be able to do a JOIN like:
select value t
from collection c
join t in c.timers
where c.id = "123"
Which would return each of the timers in the array:
[
{
"projectTimer": {
"id": 234,
"name": "My Project",
"startTime": "10:35 AM"
}
},
{
"taskTimer": {
"id": 789,
"name": "My Task",
"startTime": "10:45 AM"
}
}
]
Note the use of VALUE in the query, to strip away the containing t variable.

Related

Nodejs Customized JSON from lucid response

I have the following result set from the database, The result is an array of objects but I want it to look alike a nested objects as shown in the example.
[
{
"id": 4,
"entity": "dashboard",
"key": "message",
"value": "How to watch Netflix with XXX on Android?",
"locale": "en",
"created_at": "2022-10-25T14:41:38.000+05:00",
"updated_at": "2022-10-25T14:41:38.000+05:00"
},
{
"id": 9,
"entity": "in_survey",
"key": "content",
"value": "",
"locale": "en",
"created_at": "2022-10-25T14:41:38.000+05:00",
"updated_at": "2022-10-25T14:41:38.000+05:00"
}
]
Need a response like this from the above JSON
{
"dashboard": {
"message": "How to watch XYZ on Android?",
},
"in_survey": {
"content": ""
}
}

How to get mongoose schema result properly with missing field in JSON

I am new to Node.js and mongoose. I have tried a lot of things but not able to get the result I want.
Can someone help who knows how, who has faced this problem before or knows the solution of that problem?
Below is my database screen where 3 records are insured like below.
I have written this code and it returns the result shown below:
router.get("/categoryFields", async (request, response) => {
const categories = await categoryModel.find({}).select("parentId title slug");
try {
response.send(categories);
} catch (error) {
response.status(500).send(error);
}
});
JSON result =>
[
{
"_id": "61779e5c1e4ed11e96301ccd",
"title": "Clothing",
"slug": "hello"
},
{
"_id": "61779e8d1e4ed11e96301ccf",
"parentId": "61779e5c1e4ed11e96301ccd",
"title": "Shoe",
"slug": ""
},
{
"_id": "6177c1cd6d3e170ae58c89c3",
"title": "Electric",
"slug": ""
}
]
But I need to get a result like this - I want parentID in every object:
[
{
"_id": "61779e5c1e4ed11e96301ccd",
"parentId": "",
"title": "Clothing",
"slug": "hello"
},
{
"_id": "61779e8d1e4ed11e96301ccf",
"parentId": "61779e5c1e4ed11e96301ccd",
"title": "Shoe",
"slug": ""
},
{
"_id": "6177c1cd6d3e170ae58c89c3",
"parentId": "",
"title": "Electric",
"slug": ""
}
]
Please can anyone help me how to do this in mongoose?
db.collection.find({},
{
"parentId": {
$cond: {
if: "$parentId",
then: "$parentId",
else: ""
}
},
"title": 1,
"slug": 1
})
Test Here

How do you flatten an array of values into rows in a CosmosDB query?

Given the following collection data in a CosmosDB database:
[
{
"id": "1",
"title": "Blog post #1",
"tags": ["tag1", "tag2", "tag3"]
},
{
"id": "2",
"title": "Blog post #2",
"tags": ["tag1"]
}
]
How would you flatten this into a result set that looks like the following:
[
{
"id": "1",
"title": "Blog post #1",
"tagName": "tag1"
},
{
"id": "1",
"title": "Blog post #1",
"tagName": "tag2"
},
{
"id": "1",
"title": "Blog post #1",
"tagName": "tag3"
},
{
"id": "2",
"title": "Blog post #2",
"tagName": "tag1"
}
]
The ultimate goal in this example is finding the tag count, so this query would be wrapped in something like SELECT COUNT(1) as count, s.tagName FROM (subquery) s GROUP BY s.tagName.
You got the right idea but are using the wrong type of join. You need to use JOIN IN to flatten the array.
SELECT c.id, c.title, t AS tagName
FROM c
JOIN t IN c.tags

Rest Assured Groovy GPath filtering out result

I am trying to return the value of "description" in "dependentPreferences" where the response meets two conditions: First, check if the name is "John Smith" and then check if the "image" in preferences equals to "papaya.jpg".
The response body is as follows:
[
{
"id": 1,
"name": "John Smith",
"description": null,
"shortDescription": "No recorded interests.",
"alias": "JS",
"preferences": [
{
"id": 1,
"description": "likes candy and papaya",
"image": "papaya.jpg",
"name": "Papaya",
"dependentPreferences": [
{
"id": 1,
"description": "Fruit must be ripe",
"image": "ripe-papaya.jpg",
"name": "pap"
}
]
}
]
},
{
"id": 2,
"name": "Jane Smith",
"description": null,
"shortDescription": "No recorded interests.",
"alias": "JS",
"preferences": [
{
"id": 1,
"description": "likes candy and papaya",
"image": "papaya.jpg",
"name": "Papaya",
"dependentPreferences": [
{
"id": 1,
"description": "Candy must be Skittles",
"image": "Skittles.jpg",
"name": "skt"
}
]
}
]
}
]
So far, i have tried something like this:
response.jsonPath().getString("find { it.name == 'John Smith' }.preferences.find {it.image == 'papaya.jpg'}.dependentPreferences.description
but it is complaining about the syntax. I know this portion of the code works to find the image:
response.jsonPath().getString("find { it.name == 'John Smith' }.preferences.image
but i am having trouble constructing the second condition. Any help would be appreciated.
Working example here. This works for me:
def endpoint = "http://localhost:3130/ids.json"
def jsonResponse = get(endpoint).then().contentType(ContentType.JSON).extract().response()
def path = jsonResponse.jsonPath()
def result = path.getString("find { it.name == 'John Smith' }.preferences.find {it.image == 'papaya.jpg'}.dependentPreferences.description")
Though to be honest: in the JSON, dependentPreferences is an array of objects, so there is a chance for error if the data is different than presented.

Filter XPath to get list and leave out multiple properties with same property name

Through a XMLImport function in Excel I retrieve a JSON set of data. I would like to retrieve a list of ONLY all the names of the pools (in Example below, only 'Men Singles A' and 'Men Singles B').
Because the array of data has different properties called /name I do not know a way to filter out all the /name properties that are part of the /teams property.
[
{
"poolId": 864,
"position": 0,
"name": "Men Singles A",
"totTeams": 7,
"nrPlayersInTeam": 1,
"teams": {
"6453": {
"id": 6453,
"name": "Peter",
{
"id": 3886,
"name": "Peter",
}
]
},
"6501": {
"id": 6501,
"name": "Jack",
"players": [
{
"id": 3912,
"name": "Jack",
}
]
},
}
},
{
"poolId": 865,
"position": 1,
"name": "Men Singles B",
"totTeams": 22,
"nrPlayersInTeam": 1,
"teams": {
"6406": {
"id": 6406,
"name": "John",
"players": [
{
"id": 3844,
"name": "John",
}
]
},
"6408": {
"id": 6408,
"name": "Matthew",
"players": [
{
"id": 3845,
"name": "Matthew",
}
]
},
}
}
]
When trying the following ImportXML function with a Xpath selection, I get a list of all the /name/ properties found in the JSON array.
=IMPORTXML("https://www.tournia.net/api/v2/test/pools?tournamentUrl=test&_format=xml";"//name")
Retrieved List:
Men Singles A
Peter
Peter
Jack
Jack
Men Singles B
John
John
Mtthew
Matthew
What would be the appropriate XPath code to only get a list of all the pool names?

Resources