Query complex JSON in Postgres - postgresql

I have a json column named "Payload" with a complex json structure as below-
{
"Number": "",
"Status": "",
"Parties": [
{
"BeCode": "SHANGMAG",
"PartyCode": "CNSHANGMAGVDR",
},
{
"BeCode": "FREEMAN",
"PartyCode": "CNFREEMANVDR",
}
],
"ContactName": "test",
"Type": "",
"Legs": [
{
"Name": "",
"ELocation": {
"City": "Enns",
"State": null,
"Country": "Austria",
},
"Socation": {
"City": "Buenos Aires",
"State": null,
"Country": "Argentina",
},
"Transport": 1
},
{
"Name": "84nbt",
"ELocation": {
"City": "Linz",
"State": null,
"Country": "Austria",
},
"SLocation": {
"City": "Enns",
"State": null,
"Country": "Austria",
},
"Transport": 2
}
]
"Bookings": [
{
"BookingNo": "",
"Status": "",
"Id": ""
}
]
}
Now I need to query all the rows where SLocation is equal to ELocation.
I was able to get the "Legs" part row vise using following query -
select payload->'Legs'
from public.shipping_instruction
However, If I dig deep into the json to get the SLocation and ELocation, the query doesnt exceute.
I am looking for something like the one below-
select payload->'Legs'
from public.shipping_instruction where
payload->'Legs'->'ELocation'->'City' =
payload->'Legs'->'SLocation'->'City'
But then here the Legs have multiple SLocation and ELocation how do I handle it?

select "Number",
x."Status" as Status,
"BeCode",
"PartyCode",
"ContactName",
"Type",
"Name",
"Transport",
e_city,
e_state,
e_country,
s_city,
s_state,
s_country,
"BookingNo",
b."Status" as BookingStatus,
"Id"
from jsonb_to_record('{
"Number": "",
"Status": "",
"Parties": [
{
"BeCode": "SHANGMAG",
"PartyCode": "CNSHANGMAGVDR"
},
{
"BeCode": "FREEMAN",
"PartyCode": "CNFREEMANVDR"
}
],
"ContactName": "test",
"Type": "",
"Legs": [
{
"Name": "",
"ELocation": {
"City": "Enns",
"State": null,
"Country": "Austria"
},
"SLocation": {
"City": "Buenos Aires",
"State": null,
"Country": "Argentina"
},
"Transport": 1
},
{
"Name": "84nbtMatch",
"ELocation": {
"City": "Linz",
"State": null,
"Country": "Austria"
},
"SLocation": {
"City": "Linz",
"State": null,
"Country": "Austria"
},
"Transport": 2
},
{
"Name": "84nbt",
"ELocation": {
"City": "Linz",
"State": null,
"Country": "Austria"
},
"SLocation": {
"City": "Enns",
"State": null,
"Country": "Austria"
},
"Transport": 3
}
],
"Bookings": [
{
"BookingNo": "bn",
"Status": "bs",
"Id": "bid"
}
]
}'::jsonb) as x("Number" text,
"Status" text,
"Parties" jsonb,
"ContactName" text,
"Type" text,
"Legs" jsonb,
"Bookings" jsonb),
lateral jsonb_to_recordset(x."Parties") as p("BeCode" text, "PartyCode" text),
lateral jsonb_to_recordset(x."Legs") as l("Name" text, "Transport" int, "ELocation" jsonb, "SLocation" jsonb),
lateral (select l."ELocation" ->> 'City' as e_city,
l."ELocation" ->> 'State' as e_state,
l."ELocation" ->> 'Country' as e_country,
l."SLocation" ->> 'City' as s_city,
l."SLocation" ->> 'State' as s_state,
l."SLocation" ->> 'Country' as s_country
) loc,
lateral jsonb_to_recordset(x."Bookings") as b("BookingNo" text, "Status" text, "Id" text)
where coalesce(e_city, '') = coalesce(s_city, '')
and coalesce(e_state, '') = coalesce(s_state, '')
and coalesce(e_country, '') = coalesce(s_country, '');

Related

MondoDB $locate

I have two collections, a policy collection, and a person collection
The Policy looks something like this:
[{
"_id": {
"$oid": "61169375e2145bbfd73dedbe"
},
"PolicyNumber": {
"Identifier": "A56B096A",
"Sequence": 1
},
"Persons": {
"61169362982ac4e8ae2ad768": null,
"60de07a8dc031e91ac5f35c0": null
},
}]
The Person:
[{
"_id": {
"$oid": "5f0c6d087937e0000145e5c3"
},
"UniqueCode": "PER0004",
"PassportExpiryDate": null,
"Age": 35,
"Gender": "M",
"Nationality": null,
"LanguageCode": null,
"Name": "NAME",
"Surname": "SURNAME",
"Initials": null,
"ContactDetails": {
"Addresses": {
"physicalAddress": {
"AddressType": null,
"UnitNumber": null,
"UnitName": null,
"PoBox": null,
"StreetNumber": null,
"StreetName": null,
"Suburb": null,
"Town": null,
"PostalCode": "7100",
"Country": null,
"Coordinates": null,
"InitialCenter": null
}
},
"EmailAddresses": {
"emailAddress": "EMAIL#EMAIL.COM"
},
"TelephoneNumbers": {
"cellphone": "0000000000"
},
"PreferredCommunicationMethod": "SMS"
},
"BankingDetails": [],
"Statistics": null,
"Imported": false,
"ImportReference": null
}]
I have tried various options with $locate, $let, $convert, etc to enable me to join the two collections on 'Persons' using the Policy collections as the entry point.
I know this is probably fundamentals, but I'm n SQL guy who got dumped with a MongoDB conversion.
Any assistance will be appreciated

how to update the first object in the array of quarterFinals that matches some mongoDB condition?

I have the document below and I need to iterate through the quarterFinals array and update the first null address property I find to '123'
{
"data": {
"createTournament": {
"_id": "613122d0c7befdeef3b0b571",
"title": "Jogo de truco",
"description": "",
"location": "Batalha na rua",
"type": "Battle",
"players": [],
"status": "PENDING",
"size": 8,
"entryFee": 1,
"prizePool": 20,
"currency": "USD",
"startDate": "2021-09-01",
"endDate": "2021-09-01",
"rounds": {
"quarterFinals": [{
"id": "9fb6f9ca-c06a-4972-b140-0f2425a90707",
"arena": "",
"firstParticipant": {
"address": null,
"battlesWon": 0
},
"secondParticipant": {
"address": null,
"battlesWon": 0
}
}]
}
}
}
}
One way of doing this is
exports.updateData = async (tournamentID,address)=>{
return await db_collection.updateOne({id:tournamentId,rounds:{$elemMatch:{quarterfinals.address:null}}},{$push:{address:address});
};

how to return just one object from an array that is inside a collection with the mongo_dart

I currently have this collection below, and I need a way to return only one item from an internal array (an item from the "pontosGastronomicos" array), only the object, I don't have much experience with mongodb. I'm developing an application and AngularDart and the backend in Angel, application is already working, I just need an optimized way to do this.
// 1
{
"_id": ObjectId("6025b13eab1951094272d007"),
"id": "03a091e2-fa67-4132-9237-f5b9ed3dbb39",
"dataCadastro": "2021-02-11 19:35:41.998",
"link": null,
"icon": "icon-pizza-1",
"ativo": true,
"order": NumberInt("0"),
...
"pontosGastronomicos": [
{
"id": "208a3f93-9fcb-4db7-ac44-bb11b86a2d31",
"infos": [
{
"id": "925ed160-9276-4046-87a2-d1f37547f7cb",
"title": "BAR DA BOCA",
"lang": "pt",
"content": "Ótima experiência com o melhor chope de Rio das Ostras"
},
{
"id": "abc2ef60-4807-4b16-8d3e-95223abe0524",
"title": "BAR DA BOCA",
"lang": "en",
"content": "Great experience with the best draft beer in Rio das Ostras"
}
],
"midias": [
{
"id": "d54435d2-2fe3-4ea1-aa04-0ccdfade4c9f",
"title": "experimente-praias-1",
"description": "experimente-praias-1.jpg",
"dataCadastro": "2021-02-10 14:23:23.224",
"fisicalFilename": "acd13d50-15af-45fd-98f9-0903d1bb3ea5.jpg",
"originalFilename": "experimente-praias-1.jpg",
"link": "http://localhost:4002/storage/turismo/midias/acd13d50-15af-45fd-98f9-0903d1bb3ea5.jpg",
"mimeType": "image/jpeg",
"tipo": null
}
],
"ativo": true,
"dataCadastro": "2021-02-11 19:51:26.702",
"order": NumberInt("0"),
"link": null,
"email": "bardaboca#yahoo.com.br",
"logradouro": "Rua Teresópolis",
"bairro": "Boca da Barra",
"numero": "69",
"telefone1": "2227641671 ",
"telefone2": "22998578550",
"horarioFuncionamento": "10h as 20h",
"latitude": null,
"longitude": null,
"categoria": null,
"logo": null,
"whatsapp": null,
"tipoDeCozinha": null,
"capacidade": null,
"site": null,
"facebook": null,
"youtube": null,
"instagram": null,
"observacao": null,
}
]
}
I need to make a request that brings me just a "pontosGastronomicos" by id, i currently do this via terminal:
db.pontosGastronomicos.find(
{"pontosGastronomicos.id": "208a3f93-9fcb-4db7-ac44-bb11b86a2d31"},
{_id: 0, pontosGastronomicos: {$elemMatch: {id: "208a3f93-9fcb-4db7-ac44-bb11b86a2d31"}}});
the result is this:
{
"pontosGastronomicos": [
{
"id": "208a3f93-9fcb-4db7-ac44-bb11b86a2d31",
"infos": [
{
"id": "925ed160-9276-4046-87a2-d1f37547f7cb",
"title": "BAR DA BOCA",
"lang": "pt",
"content": "Ótima experiência com o melhor chope de Rio das Ostras"
},
{
"id": "abc2ef60-4807-4b16-8d3e-95223abe0524",
"title": "BAR DA BOCA",
"lang": "en",
"content": "Great experience with the best draft beer in Rio das Ostras"
}
],
"midias": [
{
"id": "d54435d2-2fe3-4ea1-aa04-0ccdfade4c9f",
"title": "experimente-praias-1",
"description": "experimente-praias-1.jpg",
"dataCadastro": "2021-02-10 14:23:23.224",
"fisicalFilename": "acd13d50-15af-45fd-98f9-0903d1bb3ea5.jpg",
"originalFilename": "experimente-praias-1.jpg",
"link": "http://localhost:4002/storage/turismo/midias/acd13d50-15af-45fd-98f9-0903d1bb3ea5.jpg",
"mimeType": "image/jpeg",
"tipo": null
}
],
"ativo": true,
"dataCadastro": "2021-02-11 19:51:26.702",
"order": NumberInt("0"),
"link": null,
"email": "bardaboca#yahoo.com.br",
"logradouro": "Rua Teresópolis",
"bairro": "Boca da Barra",
"numero": "69",
"telefone1": "2227641671 ",
"telefone2": "22998578550",
"horarioFuncionamento": "10h as 20h",
"latitude": null,
"longitude": null,
"categoria": null,
"logo": null,
"whatsapp": null,
"tipoDeCozinha": null,
"capacidade": null,
"site": null,
"facebook": null,
"youtube": null,
"instagram": null,
"observacao": null,
}
]
}
how to return just one object from an array that is inside a collection with the mongo_dart
I would like the result to be like this
{
"id": "208a3f93-9fcb-4db7-ac44-bb11b86a2d31",
"infos": [
{
"id": "925ed160-9276-4046-87a2-d1f37547f7cb",
"title": "BAR DA BOCA",
"lang": "pt",
"content": "Ótima experiência com o melhor chope de Rio das Ostras"
},
{
"id": "abc2ef60-4807-4b16-8d3e-95223abe0524",
"title": "BAR DA BOCA",
"lang": "en",
"content": "Great experience with the best draft beer in Rio das Ostras"
}
],
"midias": [
{
"id": "d54435d2-2fe3-4ea1-aa04-0ccdfade4c9f",
"title": "experimente-praias-1",
"description": "experimente-praias-1.jpg",
"dataCadastro": "2021-02-10 14:23:23.224",
"fisicalFilename": "acd13d50-15af-45fd-98f9-0903d1bb3ea5.jpg",
"originalFilename": "experimente-praias-1.jpg",
"link": "http://localhost:4002/storage/turismo/midias/acd13d50-15af-45fd-98f9-0903d1bb3ea5.jpg",
"mimeType": "image/jpeg",
"tipo": null
}
],
"ativo": true,
"dataCadastro": "2021-02-11 19:51:26.702",
"order": NumberInt("0"),
"link": null,
"email": "bardaboca#yahoo.com.br",
"logradouro": "Rua Teresópolis",
"bairro": "Boca da Barra",
"numero": "69",
"telefone1": "2227641671 ",
"telefone2": "22998578550",
"horarioFuncionamento": "10h as 20h",
"latitude": null,
"longitude": null,
"categoria": null,
"logo": null,
"whatsapp": null,
"tipoDeCozinha": null,
"capacidade": null,
"site": null,
"facebook": null,
"youtube": null,
"instagram": null,
"observacao": null,
}
I get the answer on github, for others who have this difficulty follow the answer:
final cursor = db.collection(collection).modernAggregateCursor([
{
r'$replaceRoot': {
'newRoot': {
r'$arrayElemAt': [
{
r'$filter': {
'input': r'$pontosGastronomicos',
'as': 'pontosGastronomicos',
'cond': {
/* resolve to a boolean value and determine if an element should be included in the output array. */
r'$eq': [r'$$pontosGastronomicos.id', '208a3f93-9fcb-4db7-ac44-bb11b86a2d31']
}
}
},
0 /* the element at the specified array index */
]
}
}
}
]);
result = await cursor.nextObject();
await cursor.close();
/*result = await db.collection(collection).modernFindOne(filter: {
'pontosGastronomicos': {
r'$elemMatch': {'id': '208a3f93-9fcb-4db7-ac44-bb11b86a2d31'}
}
}, projection: {
'_id': 0,
'pontosGastronomicos': {
r'$elemMatch': {'id': '208a3f93-9fcb-4db7-ac44-bb11b86a2d31'}
}
});*/
print(result);

Extract ? number of JSON array objects into rows

So I am trying to pivot some large Json arrays into a row by row format (postgres db). So imagine
{“searchResults”:[{“id”:“89705”,“score”:42,“title”:“Foo.”,“properties”:{…
Currently the most results we have in an array is about 300~ id's and explicitly saying;
Data::Json->'searchResults'->0->'tags'->0->>'label' as "Tag - Result 1",
...
Data::Json->'searchResults'->300->'tags'->0->>'label' as "Tag - Result
1",
Ideal Output
Array, ID , Score, Title
----
1 89705, 42, foo
1 89706, 34, bar
2 90003, 54, thing
2 98594, 53, that
(so 1,2 represent different rows in the initial table that both contain ??? amounts of objects the JSON data array)
Expanded JSON
{
"searchResults": [
{
"id": "897096",
"score": 42,
"title": "foo.",
"properties": {
"#type": "blah",
},
"publishedDate": "2018-06-30T10:20:20.555040Z",
"comments": [
{
"content": "",
"owner": {
"firstName": "",
"id": 0,
"lastName": ""
},
"id": 0,
"contentType": "",
"documentPk": 0,
"workflowStep": 0,
"order": 0
}
],
"tags": [
{
"tag": 783,
"label": "NO",
"iconClass": "",
"subGroup": "",
"exclude": false
},
{
"tag": 786,
"label": "Different name",
"iconClass": "",
"subGroup": "",
"exclude": false
}
],
"reviewTags": [
{
"tag": 2,
"label": "Accept",
"iconClass": "",
"subGroup": "",
"exclude": false
}
],
"original": {
..."names": [
{
"full_name": "This name"
}
],
"Entry Type": "Organization",
"Last Updated": "2018/05/03",
"Hit Category": "N/A",
"Aliases": [
"Olaj",
"hbhbhb"
]
},
"snippet": "",
"url": "",
"source": "_"
},
{
"id": "879057",
"score": 36,
"title": "name of company",
"properties": {
"#type": "",
"category": "SOE",
"type": "Organization",
"country": "Korea, Republic Of",
"subcategory": ""
},
"publishedDate": "2018-05-31T10:20:20.559714Z",
"comments": [
{
"content": "",
"owner": {
"firstName": "",
"id": 0,
"lastName": ""
},
"id": 0,
"contentType": "",
"documentPk": 0,
"workflowStep": 0,
"order": 0
}
],
"tags": [
{
"tag": 783,
"label": "NO",
"iconClass": "",
"subGroup": "",
"exclude": false
},
{
"tag": 786,
"label": "Different name",
"iconClass": "",
"subGroup": "",
"exclude": false
}
Any advise on what my options are here ?
Thanks #a_horse_with_no_name this worked perfectly.
it's unclear to me how the JSON continues in the array, but it seems you are looking for something like this:
with testdata(props) as (
values (
'{"searchResults":
[
{"id":"89705","score":42,"title":"Foo"},
{"id":"89706","score":34, "title":"bar"}
]}'::jsonb
)
)
select x.idx,
x.val ->> 'id' as id,
x.val ->> 'score' as score,
x.val ->> 'title' as title
from testdata, jsonb_array_elements(props -> 'searchResults') with ordinality as x (val, idx);
returns
idx | id | score | title
----+-------+-------+------
1 | 89705 | 42 | Foo
2 | 89706 | 34 | bar

Not picking Index - when querying Object -> array -> Object in N1QL

Index not picking when using N1QL to match all the customers belonging to a country
Note : the bucket has around 10 lack records, the document is fetching after 50 seconds,
query
select * from `user-prof` WHERE ANY item IN devices.location SATISFIES item.country IN ["US"]
index
CREATE INDEX id_userProfile ON `user-prof` ( ALL ARRAY v.country FOR v IN devices.location END )
Document
[
{
"customers": {
"devices": {
"user": "u1",
"custid": "14CE5534CCE",
"token": "4D5BE85896833148D696A1397C",
"guest": {
"lastActive": null,
"searches": [
]
},
"latt": "6655059908",
"locale": "en-US",
"location": [
{
"city": "Afc",
"country": "IN"
},
{
"city": "Newyork",
"country": "US"
},
{
"city": null,
"country": null
}
],
"long": "4.21787927806",
"notify": {
"Stats": false
},
"tier": null,
"tmz": "EU",
"version": "0.1"
}
}
},
{
"customers": {
"devices": {
"user": "u2",
"custid": "64CE5534CC1E",
"token": "6D5BE85896833148D696A1397C",
"guest": {
"lastActive": null,
"searches": [
]
},
"latt": "6655059908",
"locale": "en-US",
"location": [
{
"city": "Texas",
"country": "US"
},
{
"city": null,
"country": null
}
],
"long": "4.21787927806",
"notify": {
"Stats": false
},
"tier": null,
"tmz": "EU",
"version": "0.1"
}
}
}
You are using Pre 4.6.2 The variable in the ANY clause needs to be same as the variable in the CREATE INDEX (i.e item ,v )
https://developer.couchbase.com/documentation/server/current/n1ql/n1ql-language-reference/indexing-arrays.html