Postgres jsonb, nested array querying to hide specific fields - postgresql

I have a jsonb data of following format, with nested arrays
{
"outerArray": [
{
"price": {
"amount": 108.95,
"currencyCode": "GBP"
},
"innerArray": [
{
"details": {
"field1": "val1",
"field2": "val2",
"field3": "val3"
},
"otherDetail": {
"date": "2016-07-23",
"time": "19:43:00"
},
"innerMostArray": [
{
"A1": "A1"
},
{
"B1": "B1"
}
]
}
],
"someField": "values"
},
{
"price": {
"amount": 108.95,
"currencyCode": "GBP"
},
"innerArray": [
{
"details": {
"field1": "val1",
"field2": "val2",
"field3": "val3"
},
"otherDetail": {
"date": "2016-07-23",
"time": "19:43:00"
},
"innerMostArray": [
{
"A1": "A1"
},
{
"B1": "B1"
}
]
}
],
"someField": "values"
}
]
}
I want to write a retrieve query on this, to maintain same json structure but hide fields "price", "details" , "otherDetail"and "someField"
The retrieved result should look like this
{
"outerArray": [
{
"innerArray": [
{
"innerMostArray": [
{
"A1": "A1"
},
{
"B1": "B1"
}
]
}
]
},
{
"innerArray": [
{
"innerMostArray": [
{
"A1": "A1"
},
{
"B1": "B1"
}
]
}
]
}
]
}
Can this be done?

Please always specify a version of PostgreSQL you are using. An example below should work fine for versions v9.5+.
I would approach this by building a JSONB object you need with jsonb_build_object() and jsonb_build_array() functions:
Sample query:
WITH test(data) AS ( VALUES
('{
"outerArray": [
{
"price": {
"amount": 108.95,
"currencyCode": "GBP"
},
"innerArray": [
{
"details": {
"field1": "val1",
"field2": "val2",
"field3": "val3"
},
"otherDetail": {
"date": "2016-07-23",
"time": "19:43:00"
},
"innerMostArray": [
{
"A1": "A1"
},
{
"B1": "B1"
}
]
}
],
"someField": "values"
},
{
"price": {
"amount": 108.95,
"currencyCode": "GBP"
},
"innerArray": [
{
"details": {
"field1": "val1",
"field2": "val2",
"field3": "val3"
},
"otherDetail": {
"date": "2016-07-23",
"time": "19:43:00"
},
"innerMostArray": [
{
"A1": "A1"
},
{
"B1": "B1"
}
]
}
],
"someField": "values"
}
]}'::JSONB)
)
SELECT
jsonb_build_object(
'outerArray',
array_agg(
jsonb_build_object(
'innerArray',
json_build_array(
json_build_object(
'innerMostArray',
innerArray->'innerMostArray')
)
)
)
) as result
FROM test t,
jsonb_array_elements(t.data->'outerArray') as outerElement,
jsonb_array_elements(outerElement->'innerArray') as innerArray;
Result:
result
----------------------------------------------------------------------------------------------------------------------------------------------------------
{"outerArray": [{"innerArray": [{"innerMostArray": [{"A1": "A1"}, {"B1": "B1"}]}]}, {"innerArray": [{"innerMostArray": [{"A1": "A1"}, {"B1": "B1"}]}]}]}
(1 row)

Related

How to use update aggregation in MongoDb

my sample db:
"Employee": [ { "empeId": "e001",
"fName": "James",
"lName": "Bond",
"email": "jamesbond#hotmail.com",
"experience": [
"Database Design",
"SQL",
"Java" ]
},
{ "empeId": "e002",
"fName": "Harry",
"lName": "Potter",
"experience": [
"Data Warehouse",
"SQL",
"Spark Scala",
"Java Scripts" ]
} ],
"Project": [ { "projectId": "p001",
"projectTitle": "Install MongoDB" },
{ "projectId": "p002",
"projectTitle": "Install Oracle" },
{ "projectId": "p003",
"projectTitle": "Install Hadoop" } ],
"EmployeeProject": [ { "empeId": "e001",
"projectId": "p001",
"hoursWorked": 4 },
{ "empeId": "e001",
"projectId": "p003",
"hoursWorked": 2 },
{ "empeId": "e002",
"projectId": "p003",
"hoursWorked": 5 } ]
I want to update the array experience with 'test' of user with empeId: e001
desired output:
"Employee": [ { "empeId": "e001",
"fName": "James",
"lName": "Bond",
"email": "jamesbond#hotmail.com",
"experience": [
"Database Design",
"SQL",
"Java",
"test"
]
}]
I tried using
db.emp.updateOne([
{$unwind: "$Employee"},
{$match: {"Employee.emepId" : "e001" }}
],
{$push: {"Employee.experience" : "test"}})
and I get Syntax Error: Invalid property id #(shell):1:31
is this how the syntax for update with pipeline aggregation works?
The easiest and clean way(using arrayFilters):
db.collection.update({ Employee.empeId" : "e001"},
{
"$push": {
"Employee.$[x].experience": "test"
}
},
{
arrayFilters: [
{
"x.empeId": "e001"
}
]
})
Playground1
And via update/aggregation:
db.collection.update({ Employee.empeId" : "e001" },
[
{
$set: {
"Employee": {
$map: {
input: "$Employee",
as: "m",
in: {
$cond: [
{
$eq: [
"$$m.empeId",
"e001"
]
},
{
$mergeObjects: [
"$$m",
{
experience: {
$concatArrays: [
"$$m.experience",
[
"test"
]
]
}
}
]
},
"$$m"
]
}
}
}
}
}
])
Playground2

Mongodb: How to merge $facet output 2 by 2?

Playground:https://mongoplayground.net/p/YUV_fReyGsr
I have following query. I need to combine the result 2 by 2. Meaning I need to combine facet "1","2" as a result and facet "3","4" as another result. It's guaranteed that the number of facet will be even. Also, each pair of facet should get at most one record(it might not matter)
db.collection.aggregate([
{
"$facet": {
"1": [
{
$match: {
"ID": "2"
}
}
],
"2": [
{
$match: {
"array.ID": "2"
}
}
],
"3": [
{
$match: {
"array.ID": "4"
}
}
],
"4": [
{
$match: {
"ID": "4"
}
}
]
}
}
])
The expected result will be
[
{
"1": [
{
"ID": "1",
"array": [
{
"ID": "2",
"attribute1": "456"
},
{
"ID": "3",
"attribute1": "567"
}
],
"attr1": "123"
}
],
"2": [
{
"ID": "4",
"array": [
{
"ID": "5",
"attr1": "456"
}
],
"attr1": "123"
}
]
}
]
I was able to figure this out using $concatArrays operator, along with $project.
Live demo here
Database
[
{
"ID": "1",
"attr1": "123",
"array": [
{
"ID": "2",
"attribute1": "456"
},
{
"ID": "3",
"attribute1": "567"
}
]
},
{
"ID": "4",
"attr1": "123",
"array": [
{
"ID": "5",
"attr1": "456"
}
]
}
]
Query
db.collection.aggregate([
{
"$facet": {
"1": [
{
$match: {
"ID": "2"
}
}
],
"2": [
{
$match: {
"array.ID": "2"
}
}
],
"3": [
{
$match: {
"array.ID": "4"
}
}
],
"4": [
{
$match: {
"ID": "4"
}
}
]
}
},
{
"$project": {
_id: 0,
"1": {
"$concatArrays": [
"$1",
"$2"
]
},
"2": {
"$concatArrays": [
"$3",
"$4"
]
}
}
}
])
Result
[
{
"1": [
{
"ID": "1",
"_id": ObjectId("5a934e000102030405000000"),
"array": [
{
"ID": "2",
"attribute1": "456"
},
{
"ID": "3",
"attribute1": "567"
}
],
"attr1": "123"
}
],
"2": [
{
"ID": "4",
"_id": ObjectId("5a934e000102030405000001"),
"array": [
{
"ID": "5",
"attr1": "456"
}
],
"attr1": "123"
}
]
}
]

How to write an aggregate query to figure the count in Mongodb

I have the following JSON context and I have a requirement to aggregate the distinct keywords under conversion_token group and the count it is repeated; for instance:
"conversion_token": [
{
"keyword": "DBMS",
"count":4,
"classify":2
}
the keyword DBMS is used multiple times under different constructs in the json provided, the aggregate should display the
"conversion_token": [
{
"keyword": "DBMS",
"count":6,
"classify":2
}
{
"keyword": "NVL",
"count":2,`enter code here`
"classify":2
}
etc..
How can I do this?
{
"select_emp": {
"specification": {
"input": [
"p_empno"
],
"declare_stmt": {
"anchorvariable": [
"V_ENAME",
"V_HIREDATE",
"V_TITLE",
"V_REPORTSTO",
"V_DISP_DATE",
"V_INS_COUNT",
"CITY_FROM"
],
"tablename_variable": [
"EMPLOYEE.V_ENAME",
"EMPLOYEE.V_HIREDATE",
"EMPLOYEE.V_TITLE",
"EMPLOYEE.V_REPORTSTO",
"EMPLOYEE.V_DISP_DATE",
"EMPLOYEE.V_INS_COUNT",
"EMPLOYEE.CITY_FROM"
]
}
},
"body": {
"select_stmt1": {
"columns": [
"FIRSNAME",
"HIREDATE",
"TITLE",
"REPORTSTO"
],
"tablename": [
"EMPLOYEE"
],
"conversion_token": [
{
"keyword": "NVL",
"count": 1,
"classify": 2
}
]
},
"select_stmt2": {
"columns": [
"CITY"
],
"tablename": [
"EMPLOYEE"
],
"conversion_token": [
{
"keyword": "DECODE",
"count": 1,
"classify": 3
}
]
},
"dbms_stmt1": {
"dbms_putline": [
"P_EMPNO",
"V_ENAME",
"V_DISP_DATE",
"V_REPORTSTO"
],
"conversion_token": [
{
"keyword": "DBMS",
"count": 1,
"classify": 2
}
]
},
"forloop1": {
"select_stmt": {
"columns": [
"EMPLOYEEID",
"ROWID"
],
"tablename": [
"EMPLOYEE"
],
"conversion_token": [
{
"keyword": "DBMS",
"count": 1,
"classify": 2
}
]
}
},
"merge_stmt1": {
"merge_into": "EMPLOYEE",
"merge_using": {
"columns": [
"EMPLOYEEID",
"LASTNAME",
"TITLE",
"BIRTHDATE",
"HIREDATE",
"ADDRESS",
"CITY",
"STATE",
"COUNTRY",
"POSTALCODE",
"PHONE",
"FAX",
"EMAIL",
"BONUS"
],
"tablename": [
"EMPLOYEE"
]
},
"merge_update": {
"columns": [
"BONUS"
],
"tablename": [
"EMPLOYEE"
]
},
"merge_delete": {
"columns": [
"BONUS"
],
"tablename": [
"EMPLOYEE"
]
},
"merge_insert": {
"columns": [
"EMPLOYEEID",
"LASTNAME",
"FIRSTNAME",
"TITLE",
"BIRTHDATE",
"HIREDATE",
"ADDRESS",
"CITY",
"STATE",
"COUNTRY",
"POSTALCODE",
"PHONE",
"FAX",
"EMAIL",
"BONUS"
],
"tablename": [
"EMPLOYEE"
]
},
"conversion_token": [
{
"keyword": "Merge",
"count": 1,
"classify": 4
}
]
},
"exception_handling1": {
"dbms_putline": [
"P_EMPNO"
],
"conversion_token": [
{
"keyword": "DBMS",
"count": 1,
"classify": 2
}
]
}
}
}
}
First you need to concat conversion_token arrays into one using project and $concatArrays. Then using $unwind, you can separate each conversion_token object to make them ready for aggregation. After $unwind, you can group them by keyword using $group and you can take sum of count.
db.collection.aggregate([
{
$project: {
conversion_tokens: {
$concatArrays:
[
"$select_emp.body.select_stmt1.conversion_token",
"$select_emp.body.select_stmt2.conversion_token",
"$select_emp.body.dbms_stmt1.conversion_token",
"$select_emp.body.forloop1.conversion_token",
"$select_emp.body.merge_stmt1.conversion_token",
"$select_emp.body.exception_handling1.conversion_token"
]
}
}
}, {
$unwind: "$conversion_tokens"
}, {
$group: {
_id: "$conversion_tokens.keyword",
count: {
$sum: "$conversion_tokens.count"
}
}
}
])
This will produce an array like:
{
"_id": "DBMS",
"count": 3
}, {
"_id": "NVL",
"count": 1
}, {
"_id": "DECODE",
"count": 1
}
If you want to change the _id key with keyword, you can use $project again.

Filter by nested arrays/objects values (on different levels) and $project by matching groups (on a specific level) - MongoDB Aggregate

Considering I have the following collection (ignoring documents _id):
Collection
[
{
"Id": "1",
"Level2": [
{
"Id": "1.1",
"Level3": [
{
"Id": "1.1.1",
"Level4": [
{
"Id": "1.1.1.1",
"Status": "a"
},
{
"Id": "1.1.1.2",
"Status": "b"
}
]
},
{
"Id": "1.1.2",
"Level4": [
{
"Id": "1.1.2.1",
"Status": "a"
},
{
"Id": "1.1.2.2",
"Status": "c"
}
]
},
{
"Id": "1.1.3",
"Level4": [
{
"Id": "1.1.3.1",
"Status": "c"
}
]
}
]
}
]
},
{
"Id": "2",
"Level2": [
{
"Id": "2.1",
"Level3": [
{
"Id": "2.1.1",
"Level4": [
{
"Id": "2.1.1.1",
"Status": "a"
}
]
}
]
}
]
}
]
Conditions
I want to query it such as:
Level2.Id: "1.1"
Level2.Level3.Level4.Status $in ["a", "b"]
The $project should:
Group Level2.Level3.Level4 matches by Level2.Level3
Should not include Level2.Level3 if it is an empty array
The expected result should be:
Result 1
[
{
"Id": "1.1.1",
"Level4": [
{
"Id": "1.1.1.1",
"Status": "a"
},
{
"Id": "1.1.1.2",
"Status": "b"
}
]
},
{
"Id": "1.1.2",
"Level4": [
{
"Id": "1.1.2.1",
"Status": "a"
}
]
}
]
Please notice that if current Level2.Id: "2.1" has the value "1.1" instead, the result should be (matches on different documents):
Result 2
[
{
"Id": "1.1.1",
"Level4": [
{
"Id": "1.1.1.1",
"Status": "a"
},
{
"Id": "1.1.1.2",
"Status": "b"
}
]
},
{
"Id": "1.1.2",
"Level4": [
{
"Id": "1.1.2.1",
"Status": "a"
}
]
},
{
"Id": "2.1.1",
"Level4": [
{
"Id": "2.1.1.1",
"Status": "a"
}
]
}
]
How can I accomplish this with an aggregate query?
( filtering different levels and grouping by some level )
Here it is the answer (thank you to #Vijay Rajpurohit for his help on this):
db.collection.aggregate([
{
$unwind: "$Level2"
},
{
$unwind: "$Level2.Level3"
},
{
$unwind: "$Level2.Level3.Level4"
},
{
$match: {
"Level2.Id": "1.1",
"Level2.Level3.Level4.Status": {
$in: [
"a",
"b"
]
}
}
},
{
$group: {
"_id": "$Level2.Level3.Id",
"Messages": {
$push: "$Level2.Level3.Level4"
}
}
}
])
The result of this query is:
[
{
"Messages": [
{
"Id": "1.1.2.1",
"Status": "a"
}
],
"_id": "1.1.2"
},
{
"Messages": [
{
"Id": "1.1.1.1",
"Status": "a"
},
{
"Id": "1.1.1.2",
"Status": "b"
}
],
"_id": "1.1.1"
}
]
Mongo Playground

Get key value pair result of activities in Mongodb

Get key value pair result of activities. get all activities under first elements term as key.
INPUT
[
{
"_id": "diamond",
"activities": [
[
{
"term": "11",
"sport_name": "football"
}
]
]
},
{
"_id": "topaz",
"activities": [
[
{
"term": "12",
"sport_name": "football"
}
],
[
{
"term": "11",
"sport_name": "football"
},
{
"term": "11",
"sport_name": "hand ball"
}
]
]
}
]
OUTPUT
[
{
"_id": "diamond",
"activities": [
{
"11": [
{
"term": "11",
"sport_name": "football"
}
]
}
]
},
{
"_id": "topaz",
"activities": [
{
"12": [
{
"term": "12",
"sport_name": "football"
}
]
},
{
"11": [
{
"term": "11",
"sport_name": "football"
},
{
"term": "11",
"sport_name": "hand ball"
}
]
}
]
}
]
You can use below aggregation
db.collection.aggregate([
{ "$project": {
"activities": {
"$arrayToObject": {
"$map": {
"input": "$activities",
"in": {
"k": { "$arrayElemAt": ["$$this.term", 0] },
"v": "$$this"
}
}
}
}
}}
])
MongoPlayground