How to query the jsonb path of the given nested json data which shows expense details with name of customer - postgresql

[
{
"WHO": "Joe",
"WEEK": [
{
"NUMBER": 3,
"EXPENSE": [
{
"WHAT": "Beer",
"AMOUNT": 18.00
},
{
"WHAT": "Food",
"AMOUNT": 12.00
},
{
"WHAT": "Food",
"AMOUNT": 19.00
},
{
"WHAT": "Car",
"AMOUNT": 20.00
}
]
},
{
"NUMBER": 4,
"EXPENSE": [
{
"WHAT": "Beer",
"AMOUNT": 19.00
},
{
"WHAT": "Beer",
"AMOUNT": 16.00
},
{
"WHAT": "Food",
"AMOUNT": 17.00
},
{
"WHAT": "Food",
"AMOUNT": 17.00
},
{
"WHAT": "Beer",
"AMOUNT": 14.00
}
]
},
{
"NUMBER": 5,
"EXPENSE": [
{
"WHAT": "Beer",
"AMOUNT": 14.00
},
{
"WHAT": "Food",
"AMOUNT": 12.00
}
]
}
]
},
{
"WHO": "Beth",
"WEEK": [
{
"NUMBER": 3,
"EXPENSE": [
{
"WHAT": "Beer",
"AMOUNT": 16.00
}
]
},
{
"NUMBER": 4,
"EXPENSE": [
{
"WHAT": "Food",
"AMOUNT": 17.00
},
{
"WHAT": "Beer",
"AMOUNT": 15.00
}
]
},
{
"NUMBER": 5,
"EXPENSE": [
{
"WHAT": "Food",
"AMOUNT": 12.00
},
{
"WHAT": "Beer",
"AMOUNT": 20.00
}
]
}
]
},
{
"WHO": "Janet",
"WEEK": [
{
"NUMBER": 3,
"EXPENSE": [
{
"WHAT": "Car",
"AMOUNT": 19.00
},
{
"WHAT": "Food",
"AMOUNT": 18.00
},
{
"WHAT": "Beer",
"AMOUNT": 18.00
}
]
},
{
"NUMBER": 4,
"EXPENSE": [
{
"WHAT": "Car",
"AMOUNT": 17.00
}
]
},
{
"NUMBER": 5,
"EXPENSE": [
{
"WHAT": "Beer",
"AMOUNT": 14.00
},
{
"WHAT": "Car",
"AMOUNT": 12.00
},
{
"WHAT": "Beer",
"AMOUNT": 19.00
},
{
"WHAT": "Food",
"AMOUNT": 12.00
}
]
}
]
}
]

Related

mongodb update where argument exists

db.filmes.insertMany(
[
{
"num": 1,
"title": "movie1",
"category": "Horror",
"review": 5,
"cast": {
"director": "R1",
"artist": "A1"
},
"exibition": [
"Lisboa",
"Porto",
"Braga"
]
},
{
"num": 2,
"title": "movie2",
"category": "Action",
"review": 3,
"cast": {
"director": "R2",
"artist": "A2"
},
"exibition": [
"Lisboa",
"Porto"
]
},
{
"num": 3,
"title": "movie3",
"category": "Animation",
"review": 3,
"cast": {
"director": "R2",
"artist": "A2"
},
"exibition": [
"Lisboa",
"Porto",
"Viseu"
]
},
{
"num": 4,
"title": "movie4",
"category": "Animation",
"review": 6,
"cast": {
"director": "R1",
"artist": "A4"
},
"exibition": [
"Lisboa",
"Porto",
"Braga"
]
},
{
"num": 5,
"title": "movie5",
"category": "Horror",
"review": 5,
"cast": {
"director": "R3",
"artist": "A3"
},
"exibition": [
"Lisboa",
"Porto"
]
}
]);
I need to update all the reviews to 10 where artist "A3" appears?
I already tried :
db.movies.updateMany({cast:{artist:"A3"}},{$set:{review:10}})
db.movies.update({artist:"A3"},{$ste:{review:10}});
and other that I cant remember,
there's is a way to do it ?

How to sum the balance and income using MongoDB?

I have a document exchange_history like:
[
{
"_id": {"$oid": "611fd7059ec255088af94e4d"},
"amount": {"$numberDecimal": 1.2},
"exchange_time": {"$date": "2021-08-20T16:02:00.448Z"},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {"$oid": "611fed2a31ef85394c4f9b10"},
"amount": {"$numberDecimal": 1.02},
"exchange_time": {"$date": "2021-08-20T16:02:00.448Z"},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {"$oid": "6120b47d4cf35f7f70312a71"},
"amount": {"$numberDecimal": -1},
"exchange_time": {"$date": "2021-08-21T08:08:16.397Z"},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {"$oid": "6120b4834cf35f7f70312a72"},
"amount": {"$numberDecimal": -2},
"exchange_time": {"$date": "2021-08-21T08:08:16.397Z"},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {"$oid": "6120b4e94cf35f7f70312a73"},
"amount": {"$numberDecimal": 10},
"exchange_time": {"$date": "2021-08-21T08:08:16.397Z"},
"user_id": "611df4d9ece358586d212d91"
}
]
How to calculate the balance (sum amount) and income (sum amount where amount is greater than 0) by user_id at the same time using MongoDB?
My MongoDB version is 4.4. Pls help me!
Try this ...
Document :
[
{
"_id": {
"oid": "611fd7059ec255088af94e4d"
},
"amount": {
"numberDecimal": 1.2
},
"exchange_time": {
"date": "2021-08-20T16:02:00.448Z"
},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {
"oid": "611fed2a31ef85394c4f9b10"
},
"amount": {
"numberDecimal": 1.02
},
"exchange_time": {
"date": "2021-08-20T16:02:00.448Z"
},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {
"oid": "6120b47d4cf35f7f70312a71"
},
"amount": {
"numberDecimal": -1
},
"exchange_time": {
"date": "2021-08-21T08:08:16.397Z"
},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {
"oid": "6120b4834cf35f7f70312a72"
},
"amount": {
"numberDecimal": -2
},
"exchange_time": {
"date": "2021-08-21T08:08:16.397Z"
},
"user_id": "60ef435299b1760752cbafe1"
},
{
"_id": {
"oid": "6120b4e94cf35f7f70312a73"
},
"amount": {
"numberDecimal": 10
},
"exchange_time": {
"date": "2021-08-21T08:08:16.397Z"
},
"user_id": "611df4d9ece358586d212d91"
}
]
Code :
db.collection.aggregate([
{
$group: {
_id: "$user_id",
balance: {
$sum: "$amount.numberDecimal"
},
income: {
$sum: {
$cond: [
{
$gt: [
"$amount.numberDecimal",
0
]
},
"$amount.numberDecimal",
0
]
}
}
}
},
])
And Output:
[
{
"_id": "611df4d9ece358586d212d91",
"balance": 10,
"income": 10
},
{
"_id": "60ef435299b1760752cbafe1",
"balance": -0.78,
"income": 2.2199999999999998
}
]
Try mongoplayground

mongodb agregate and filter data

I try to filter some results data from mongodb with mongoose in javascript.
This is my json structure:
{
"name": "john",
"firstname": "doe",
"yearold": 22,
"recipes": [
{
"title": "cheesecake",
"data": [
{
"name": "egg",
"label": "Eggs for",
"value": 6,
"unit": "piece"
},
{
"name": "oil",
"label": "Specific oil",
"unit": "oz",
"value": 0.2
},
{
"name": "flour",
"label": "Wholemel flour",
"value": 450,
"unit": "gr"
}
]
},
{
"title": "cake",
"data": [
{
"name": "egg",
"label": "Eggs for",
"value": 6,
"unit": "piece"
},
{
"name": "flour",
"label": "Wholemel flour",
"value": 500,
"unit": "gr"
},
]
}
]
}
In some case i need to return json data with hiding some values. For example I have a list that specifies all the values ​​to hide
hidekeys=["egg"];
and i would like to get this:
{
"name": "john",
"firstname": "doe",
"yearold": 22,
"recipes": [
{
"title": "cheesecake",
"data": [
{
"name": "egg",
"label": "Eggs for",
"value": #######,
"unit": "piece"
},
{
"name": "oil",
"label": "Specific oil",
"unit": "oz",
"value": 0.2
},
{
"name": "flour",
"label": "Wholemel flour",
"value": 450,
"unit": "gr"
}
]
},
{
"title": "cake",
"data": [
{
"name": "egg",
"label": "Eggs for",
"value": #######,
"unit": "piece"
},
{
"name": "flour",
"label": "Wholemel flour",
"value": 500,
"unit": "gr"
},
]
}
]
}
For each recipe i need to hide ingredient value if it is specified in hidekeys.
I tried something with $project and $cond but it doesnt works
Here's a quick way of how to achieve this using $map
const hidekeys = ["egg"];
db.collection.aggregate([
{
$addFields: {
recipes: {
$map: {
input: "$recipes",
as: "recipe",
in: {
$mergeObjects: [
"$$recipe",
{
data: {
$map: {
input: "$$recipe.data",
as: "datum",
in: {
"$mergeObjects": [
"$$datum",
{
$cond: [
{
"$setIsSubset": [
[
"$$datum.name"
],
hidekeys
]
},
{
value: "#####"
},
{
value: "$$datum.value"
}
]
}
]
}
}
}
}
]
}
}
}
}
}
])
Mongo Playground

Why are some of my Google Vision API DOCUMENT_TEXT_DETECTION results' normalizedVertices greater than 1?

I have implemented an OCR invoice solution using Google Vision API DOCUMENT_TEXT_DETECTION service. The software has been in production for a couple months. 99% of the time, the results are pretty accurate. But every once in a while, I get some results containing normalizedVertices with points greater than 1. According to the docs, a normalized vertex should be between 0 and 1. Anyone know why this happens? And how these should be interpreted?
Only thing I have noticed (in conjunction with this issue) is that often the documents producing these results may be rotated. But I don't see why rotation would cause the normalization to be off.
Here's a sample file
{
"fullTextAnnotation": {
"pages": [
{
"property": {
"detectedLanguages": [
{
"languageCode": "en",
"confidence": 0.84
},
{
"languageCode": "da",
"confidence": 0.01
}
]
},
"width": 612,
"height": 792,
"blocks": [
{
"boundingBox": {
"normalizedVertices": [
{
"x": 1.2254902,
"y": 0.79545456
},
{
"x": 1.2254902,
"y": 0.9166667
},
{
"x": 1.1960784,
"y": 0.9166667
},
{
"x": 1.1960784,
"y": 0.79545456
}
]
},
"paragraphs": [
{
"boundingBox": {
"normalizedVertices": [
{
"x": 1.2254902,
"y": 0.79545456
},
{
"x": 1.2254902,
"y": 0.9166667
},
{
"x": 1.1960784,
"y": 0.9166667
},
{
"x": 1.1960784,
"y": 0.79545456
}
]
},
"words": [
{
"property": {
"detectedLanguages": [
{
"languageCode": "en"
}
]
},
"boundingBox": {
"normalizedVertices": [
{
"x": 1.2254902,
"y": 0.79545456
},
{
"x": 1.2254902,
"y": 0.9166667
},
{
"x": 1.1960784,
"y": 0.9166667
},
{
"x": 1.1960784,
"y": 0.79545456
}
]
},
"symbols": [
{
"property": {
"detectedLanguages": [
{
"languageCode": "en"
}
]
},
"text": "I",
"confidence": 0.99
},
{
"property": {
"detectedLanguages": [
{
"languageCode": "en"
}
]
},
"text": "N",
"confidence": 1
},
{
"property": {
"detectedLanguages": [
{
"languageCode": "en"
}
]
},
"text": "V",
"confidence": 0.99
},
{
"property": {
"detectedLanguages": [
{
"languageCode": "en"
}
]
},
"text": "O",
"confidence": 0.99
},
{
"property": {
"detectedLanguages": [
{
"languageCode": "en"
}
]
},
"text": "I",
"confidence": 0.99
},
{
"property": {
"detectedLanguages": [
{
"languageCode": "en"
}
]
},
"text": "C",
"confidence": 0.99
},
{
"property": {
"detectedLanguages": [
{
"languageCode": "en"
}
],
"detectedBreak": {
"type": "LINE_BREAK"
}
},
"text": "E",
"confidence": 1
}
],
"confidence": 0.99
}
],
"confidence": 0.99
}
],
"blockType": "TEXT",
"confidence": 0.99
},...

Postgresql json query

I have one table with one column jsonb with the text:
[
{
"id": 1,
"nome": "AUTOMÓVEL",
"partes": [
{
"fracao": 100,
"documento": "111.111.111-11"
}
],
"atributos": [
{
"id": 3,
"nome": "MARCA",
"valor": "FORD"
},
{
"id": 2,
"nome": "MODELO",
"valor": "FIESTA"
},
{
"id": 1,
"nome": "RENAVAM",
"valor": "RENAVAM"
}
]
},
{
"id": 1,
"nome": "AUTOMÓVEL",
"partes": [
{
"fracao": 100,
"documento": "111.111.111-11"
}
],
"atributos": [
{
"id": 3,
"nome": "MARCA",
"valor": "FIAT"
},
{
"id": 2,
"nome": "MODELO",
"valor": "PALIO"
},
{
"id": 1,
"nome": "RENAVAM",
"valor": "RENAVAM"
}
]
},
{
"id": 1,
"nome": "AUTOMÓVEL",
"partes": [
{
"fracao": 100,
"documento": "111.111.111-11"
}
],
"atributos": [
{
"id": 3,
"nome": "MARCA",
"valor": "HONDA"
},
{
"id": 2,
"nome": "MODELO",
"valor": "FIT"
},
{
"id": 1,
"nome": "RENAVAM",
"valor": "RENAVAM"
}
]
}
]
so i create a select:
select objetos
from protocolo
where objetos #> '[{"id": 1 , "atributos": [{"nome": "MARCA", "valor" : "FIAT"}]}]'
but this select will return the column with all text, but I need to return only the element that match in my where clause
so I need to return only this:
{
"id": 1,
"nome": "AUTOMÓVEL",
"partes": [
{
"fracao": 100,
"documento": "111.111.111-11"
}
],
"atributos": [
{
"id": 3,
"nome": "MARCA",
"valor": "FIAT"
},
{
"id": 2,
"nome": "MODELO",
"valor": "PALIO"
},
{
"id": 1,
"nome": "RENAVAM",
"valor": "RENAVAM"
}
]
}
Use the function jsonb_array_elements():
select value
from protocolo
cross join lateral jsonb_array_elements(objetos)
where value #> '{"id": 1 , "atributos": [{"nome": "MARCA", "valor" : "FIAT"}]}';
Note that the json expression in the where clause has changed.