I am creating a service using cosmos db.I am trying to creating a search query.
Query :
SELECT product.Name,product1.Name
FROM catalog
join industry in catalog.Industy
join category in industry.Category
join product1 in category.Product
join Subcategory in category.Subcategory
join product in Subcategory.Product
WHERE CONTAINS(product1.Name,'dg')
But i can not able to get both product and product list . it give me the error. Name already used.
error:
Object creation error, property name 'Name' specified more than once
Tree that i am trying to fetch :
[
{
"id": "string",
"industy": [
{
"id": "string",
"category": [
{
"id": "string",
"subcategory": [
{
"id": "string",
"product": [
{
"id": "string",
"methodOfPreparation": [
{
"id": "string",
}
],
"addons": [
{
"id": "string"
}
]
}
]
}
],
"product": [
{
"id": "string",
"methodOfPreparation": [
{
"id": "string"
}
],
"addons": [
{
"id": "string"
}
]
}
]
}
]
}
]
}
]
expect Output
product[],prodcut1[]
How can i solve this?
Firstly, the error could be solved if you use alias as #Zohar mentioned in the comment.
SELECT product.Name as productName,product1.Name as product1Name
FROM catalog
join industry in catalog.industy
join category in industry.category
join product1 in category.product
join Subcategory in category.subcategory
join product in Subcategory.product
The reason is that every retrieved item is an obj, the format of results is an array consists of many objects. The object can't accept duplicate column names.
If you want to get the format like product[],prodcut1[],you need to loop the result and assemble by yourself.(For example,use stored procedure)
Related
Supposed I have this schema
class Room {
member_ids: [String]
owner_ids: [String]
}
And two virtual populates members and owners, which map to User schema (custom path, not _id)
I successfully get the data populated with this:
return this.roomModel
.findOne({ id: roomId })
.select('-_id -__v')
.populate('members owners', '-_id -__v')
.exec();
It now returns
{
"member_ids": [
"1",
"2"
],
"owner_ids": [
"1"
],
"owners": [
{
"id": "1",
"name": "User 1"
}
],
"members": [
{
"id": "1",
"name": "User 1"
},
{
"id": "2",
"name": "User 2"
}
]
}
The thing is, I don't want member_ids and owner_ids to end up in my response. I've tried using select('-member_ids -owner_ids') however the response did not have populated data anymore (I guess the select phase happens before the populate phase?). Is there anyway to achieve this, without resorting to manually removing the fields afterwards? Thank you.
Starting out with JSONB data type and I'm hoping someone can help me out.
I have a table (properties) with two columns (id as primary key and data as jsonb).
The data structure is:
{
"ProductType": "ABC",
"ProductName": "XYZ",
"attributes": [
{
"name": "Color",
"type": "STRING",
"value": "Silver"
},
{
"name": "Case",
"type": "STRING",
"value": "Shells"
},
...
]
}
I would like to update the value of a specific attributes element by name for a row with a given id. For example, for the element with "name"="Case" change the value to "Glass". So it ends up like
{
"ProductType": "ABC",
"ProductName": "XYZ",
"attributes": [
{
"name": "Color",
"type": "STRING",
"value": "Silver"
},
{
"name": "Case",
"type": "STRING",
"value": "Glass"
},
...
]
}
Is this possible with this structure using SQL?
I have created table structure if any of you would like to give it a shot.
dbfiddle
Use the jsonb concatenation operator, ||, to replace keys on the fly:
WITH properties (id, data) AS (
values
(1, '{"ProductType": "ABC","ProductName": "XYZ","attributes": [{"name": "Color","type": "STRING","value": "Silver"},{"name": "Case","type": "STRING","value": "Shells"}]}'::jsonb),
(2, '{"ProductType": "ABC","ProductName": "XYZ","attributes": [{"name": "Color","type": "STRING","value": "Red"},{"name": "Case","type": "STRING","value": "Shells"}]}'::jsonb)
)
SELECT id,
data||
jsonb_build_object(
'attributes',
jsonb_agg(
case
when attribs->>'name' = 'Case' then attribs||'{"value": "Glass"}'::jsonb
else attribs
end
)
) as data
FROM properties m
CROSS JOIN LATERAL JSONB_ARRAY_ELEMENTS(data->'attributes') as a(attribs)
GROUP BY id, data
Updated fiddle
I have a JSONB column in my PostgreSQL database. The data looks like this:
{
"cars":
[
{
"id": 1,
"brand": "BMW"
"parts":
[
{
"partId": 5,
"type": "battery"
}
]
},
{
"id": 2,
"brand": "Mercedes"
"parts":
[
{
"partId": 5,
"type": "battery"
},
{
"partId": 6,
"type": "engine"
}
]
}
]
}
Is there any way that I can search for all cars that have a part with type "battery"? How can I search inside of cars array and then inside of the parts array of each car element?
As it's not clear in your question that what output you want. So I am assuming that you want id and brand name in output:
so you try this:
select distinct x.y->>'id', x.y->>'brand'
from test
cross join lateral jsonb_array_elements(data->'cars') x(y)
cross join lateral jsonb_array_elements(x.y->'parts') a(b)
where a.b->>'type'='battery'
DEMO
I have a table of data which has a field called details which contains a json object.
The object looks something like this:
{
"name": "Persons Name",
"list": [
{
"name": "Persons Name",
"assigned": {
"company": "Company 1",
"number": "AA1"
}
},
{
"name": "Persons Name",
"assigned": {
"company": "Company 2",
"number": "BB2"
}
},
{
"name": "Persons Name",
"assigned": {
"company": "Company 3",
"number": "AA3"
}
}
],
"total_results": 3
}
Essentially, I want to return all data if any of the person's 'assigned'->>'number' field begins with an A. In the above example, two of the individuals numbers are prefixed with an A so I want all data returned.
I've been playing around and had have been making some progress but can't figure out how to bring it all together.
select f->'assigned'->>'number' from jsonb_array_elements((select details->'list' from table_name)) f;
The above query can get me a list of the three 'number' fields but I'm not sure how I can combine that with a query to return all the information, if any of these fields contain a prefix A
You are so close. Just add your condition in where clause.
SELECT *
FROM JSON_ARRAY_ELEMENTS((SELECT details -> 'list' FROM TABLE_NAME)) f
WHERE f -> 'assigned' ->> 'number' LIKE 'A%'
CHECK DEMO HERE
I am attempting to store a connected Node's data in a property of a selected Node in OrientDB via the OUT() projection. e.g.:
SELECT *, OUT("Has_Friend") AS Friends FROM Person
Given that a "Person" Node is connected to several "Friend" Nodes via the "Has_Friend" Edge, I would like the actual Friend Node properties to be stored in the "Friends" property on each Person Node returned by this query. e.g.:
{
"result": [
{
"Name": "Joe",
"Friends": [
{
"Name": "Ben",
"Title": "Mr."
},
{
"Name": "Stan",
"Title": "Dr."
}
]
},
{
"Name": "Tim",
"Friends": [
{
"Name": "Terrance",
"Title": "Esq."
},
{
"Name": "Sarah",
"Title": "Dr."
}
]
}
]
}
However, the query only stores the RID of each "Friend" Node in the "Friends" property rather than the actual data of that "Friend" Node. e.g.:
{
"result": [
{
"Name": "Joe",
"Friends": [
"#228:1",
"#227:1"
]
},
{
"Name": "Tim",
"Friends": [
"#225:1",
"#226:1"
]
}
]
}
I've searched the OrientDB documentation but am unsure as to how I might accomplish this. I suspect there's a way to nest queries for those Friend nodes inside of the primary query, but I'm not entirely sure how to do that. Any insight is greatly appreciated!
try to use expand() function. It would expands the document pointed by that link and give all properties of this document. So your query should looks like this one:
SELECT expand(in("Has_Friend")) AS Friend FROM Person