Here are 3 entities
ProjectEntity
{_id:String,projectName:String,users:[userEntityId1,userEntityId2]}
UserEntity
{_id:String,userName:String,medias:[mediaEntityId1,mediaEntityId2]}
MediaEntity
{_id:String,url:String}
as description above,I storage foreign id as simple string array to field,I have no idea how to query a result as Json like below:
{
projectName:"demoProject",
users:[
{
userName:"Joey",
medias:[
{url:"https://whatever1.png"},
{url:"https://whatever2.png"},
]
},
{
userName:"Chandler",
medias:[
{url:"https://whatever1.png"},
{url:"https://whatever2.png"},
]
}
]
}
is any goodman can help me? thank you!
Related
I use NestJj,PostgreSQL and Graphql for Back-End,
Flutter,graphql_flutter for Front End.
I have a collection store like this :
I want to get the following result:
[
{
type:'A',
details:[
{name:'Food1'}
]
},
{
type:'Expense',
details:[
{name:'gym'},
{name:'Dinner'}
]
},
{
type:'Revenue',
details:[
{name:'Revenue'}
]
}
]
For show on the device :
How can I query?
Could you help me?
I'm not sure if you'll be able to build that structure at the level of SQL.
What you can do is to extract the data from the table wit the structure as it is and then map it at the level of JS.
// here's an example with TypeORM
const data = await Collection.findBy({...});
const result = data.map(item => ({
type: item.type,
details: [{
name: item.name
}]
}));
P.S. I'm pretty sure that's not the answer you've expected but it will solve your issue.
I have below table in postgresql which stored JSON data in jsonb type of column.
CREATE TABLE "Trial" (
id SERIAL PRIMARY KEY,
data jsonb
);
Below is the sample json structure
{
"id": "000000007001593061",
"core": {
"groupCode": "DVL",
"productType": "ZDPS",
"productGroup": "005001000"
},
"plants": [
{
"core": {
"mrpGroup": "ZMTS",
"mrpTypeDesc": "MRP",
"supLeadTime": 777
},
"storageLocation": [
{
"core": {
"storageLocation": "H050"
}
},
{
"core": {
"storageLocation": "H990"
}
},
{
"core": {
"storageLocation": "HM35"
}
}
]
}
],
"discriminator": "Material"
}
These are the scripts for insert json data
INSERT INTO "Trial"(data)
VALUES(CAST('{"id":"000000007001593061","core":{"groupCode":"DVL","productType":"ZDPS","productGroup":"005001000"},"plants":[{"core":{"mrpGroup":"ZMTS","mrpTypeDesc":"MRP","supLeadTime":777},"storageLocation":[{"core":{"storageLocation":"H050"}},{"core":{"storageLocation":"H990"}},{"core":{"storageLocation":"HM35"}}]}],"discriminator":"Material"}' AS JSON))
INSERT INTO "Trial"(data)
VALUES(CAST('{"id":"000000000104107816","core":{"groupCode":"ELC","productType":"ZDPS","productGroup":"005001000"},"plants":[{"core":{"mrpGroup":"ZCOM","mrpTypeDesc":"MRP","supLeadTime":28},"storageLocation":[{"core":{"storageLocation":"H050"}},{"core":{"storageLocation":"H990"}}]}],"discriminator":"Material"}' AS JSON))
INSERT INTO "Trial"(data)
VALUES(CAST('{"id":"000000000104107818","core":{"groupCode":"DVK","productType":"ZDPS","productGroup":"005001000"},"plants":[{"core":{"mrpGroup":"ZMTL","mrpTypeDesc":"MRP","supLeadTime":28},"storageLocation":[{"core":{"storageLocation":"H050"}},{"core":{"storageLocation":"H990"}}]}]}' AS JSON))
If try to sort by at first level then it works
select id,data->'core'->'groupCode'
from "Trial"
order by data->'core'->'groupCode' desc
But when I try to sort by at nested level, below is the script then it doesn't work for me, I'm for sure I'm wrong for this script but don't know what is it ? Need assistant if someone knows how to order by at nested level for JSONB data.
select id,data->'plants'
from sap."Trial"
order by data->'plants'->'core'->'mrpGroup' desc
Need assistance for write a query for order by at nested level for JSONB data.
Below query works for me
SELECT id, data FROM "Trial" ORDER BY jsonb_path_query_array(data, '$.plants[*].core[*].mrpGroup') desc limit 100
I have a case when my data in in nested arrays of jsonb in order to find the value I have to do multiple JSONB_ARRAY_ELEMENTS which is costly and takes a lots of nested code.
The json file has the continents inside countries and inside cities.
I need to access a city value.
Is there a way to make this query simpler and faster?
I was trying to solve it using JSON_EXTRACT_PATH but in order to get in to a array but I need the indexes.
WITH mydata AS (
SELECT '
{
"continents":[
{
"name":"America",
"area":43316000,
"countries":[
{
"country_name":"Canada",
"capital":"Toronto",
"cities":[
{
"city_name":"Ontario",
"population":2393933
},
{
"city_name":"Quebec",
"population":12332
}
]
},
{
"country_name":"Brazil",
"capital":"Brasilia",
"cities":[
{
"city_name":"Sao Paolo",
"population":34534534
},
{
"city_name":"Rio",
"population":445345
}
]
}
]
},
{
"name":"Europa",
"area":10530751,
"countries":[
{
"country_name":"Switzerland",
"capital":"Zurich",
"cities":[
{
"city_name":"Ginebra",
"population":4564565
},
{
"city_name":"Basilea",
"population":4564533
}
]
},
{
"country_name":"Norway",
"capital":"Oslo",
"cities":[
{
"city_name":"Oslo",
"population":3243534
},
{
"city_name":"Steinkjer",
"population":4565465
}
]
}
]
}
]
}
'::JSONB AS data_column
)
SELECT cit.city->>'city_name' AS city,
(cit.city->>'population')::INTEGER AS population
FROM (SELECT JSONB_ARRAY_ELEMENTS(coun.country->'cities') AS city
FROM (SELECT JSONB_ARRAY_ELEMENTS(cont.continent->'countries') AS country
FROM (SELECT JSONB_ARRAY_ELEMENTS(data_column->'continents') AS continent
FROM mydata
) AS cont
WHERE cont.continent #> '{"name":"Europa"}'
) AS coun
WHERE coun.country #> '{"country_name" : "Norway"}'
) AS cit
WHERE cit.city #> '{"city_name": "Oslo"}'
See my nested queries? looks ugly, I can get the answer using: JSONB_EXTRACT_PATH( data_column->'continents', '1', 'countries', '1', 'cities', '0', 'population') but I had to hardcode the array indexes.
Hope you can help me out.
Thanks.
You don't need any nesting, you can do lateral queries:
SELECT
city->>'city_name' AS city,
(city->>'population')::INTEGER AS population
FROM
mydata,
JSONB_ARRAY_ELEMENTS(data_column->'continents') AS continent,
JSONB_ARRAY_ELEMENTS(continent->'countries') AS country,
JSONB_ARRAY_ELEMENTS(country->'cities') AS city
WHERE continent ->> 'name' = 'Europa'
AND country ->> 'country_name' = 'Norway'
AND city ->> 'city_name' = 'Oslo';
(online demo)
However, since you mentioned paths and having to specify indices in there, this is actually the perfect use case for Postgres 12 JSON paths:
SELECT jsonb_path_query(data_column, '$.continents[*]?(#.name == "Europa").countries[*]?(#.country_name=="Norway").cities[*]?(#.city_name=="Oslo")') FROM mydata
(online demo)
I want to extract keys from nested json using spark.
I have below JSON
{
"predicates": {
"API_No": "http://www.oilandgas.com/api_no",
"Facility_ID": "http://www.oilandgas.com/facility_id"
},
"prefixes": {
"API_No": "http://www.oilandgas.com/api_no/ ",
"Facility_ID": "http://www.oilandgas.com/facility_id/ "
},
"relations": {
"API_No": [
"Facility_ID",
"County"
]
},
"grahName": "http://www.oilandgas.com/data"
}
I wrote below code read json
val df = spark.read.option("multiline", "true").json("path/to/above/json")
df.select(explode(array(col("relations")))).columns.foreach(println)
I want to get key in 'relations' as 'API_No' from dataframe.
Thanks In advance.
For getting the key in relations as API_No from dataframe you have to just project (select) just relations key. Since type of relations key is struct, by projecting it you can get the desired result. Like the following:
df.select("relations.*").columns.foreach(println)
It will give the following result:
API_No
I hope it helps!
I am using Laravel with mongodb, i have little understanding in laravel eloquent relationship,currently my collection structure is as follows
collection name:general_details
{
"id": 01,
"personal_details":[
[
"emp_id":10,
"blood_group":"B+ve"
],
[
"emp_id":11,
"blood_group":"B+ve"
]
]
}
collection name:employee_details
{
"emp_id":10,
"emp_name":"Alex"
},
{
"emp_id":11,
"emp_name":"Ramesh"
}
i want to create eloquent relationship between two collections for "emp_id", please suggest any solution?
In GenDetails model
public function empdetails(){
return $this->hasOne('App\EmpDetails');
}
In EmpDetails Model
public function genDetails(){
return $this->belongsTo('App\GenDetails');
}
here is one to one relationship between GenDetails and EmpDetails model. So learn more from laravel documentation for Eloquent Relationship.
In GenDetail Model put this relationship
public function empDetails(){
return $this->hasMany('App\EmpDetails','emp_id','personal_details.emp_id');
}
i think this relationship will defiantly work for you.