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.
Related
I'm using sequelizejs to generate a report with these models:
class Employees extends Model {
static associate(models) {
this.belongsToMany(models.Departaments, {
through: models.DepartamentsEmployees
});
this.belongsToMany(models.CostCenters, {
through: models.CentersEmployees
})
}
}
class CostCenters extends Model {
static associate(models) {
this.belongsToMany(models.Employees, {
through: models.CentersEmployees
});
}
}
class Departaments extends Model {
static associate(models) {
this.belongsToMany(models.Employees, {
through: models.DepartamentsEmployees
});
}
}
The question is that in the report I have to order the information with this order CostCenter->Departments->Employees, for example, If I have this information:
{
"id": 3,
"name": "Peru",
"employeesCount": "2",
"Employees": [
{
"id": "5801c217-4db8-40d0-aace-c936f3fa7c0d",
"name": "Julian",
"email": "augusto#gmail.com",
"Departaments": [
{
"id": 2,
"name": "Gerencia"
}
]
},
{
"id": "7a24a8d9-77b8-4bd3-a6d7-0053ca3d6357",
"name": "Hernesto",
"email": "mango#gmail.com",
"Departaments": [
{
"id": 1,
"name": "Marketing"
}
]
},
{
"id": "5b24a6f0-75b8-4bd3-p6d7-0053ca3c6357",
"name": "Maria",
"email": "maria#gmail.com",
"Departaments": [
{
"id": 1,
"name": "Marketing"
}
]
}
]
}
The information should have to be ordered like this:
{
"id": 3,
"name": "Peru",
"employeesCount": "2",
"Departaments": [
{
"id": 2,
"name": "Gerencia",
"Employees": [
{
"id": "5801c217-4db8-40d0-aace-c936f3fa7c0d",
"name": "Julian",
"email": "augusto#gmail.com"
}
]
},
{
"id": 1,
"name": "Marketing",
"Employees": [
{
"id": "5b24a6f0-75b8-4bd3-p6d7-0053ca3c6357",
"name": "Maria",
"email": "maria#gmail.com"
},
{
"id": "7a24a8d9-77b8-4bd3-a6d7-0053ca3d6357",
"name": "Hernesto",
"email": "mango#gmail.com"
}
]
}
]
}
I'm running the query with this:
return await models.CostCenters.findAll({
include: [
{
model: models.Employees,
through: {attributes: []},
include: [{
model: models.Departaments,
through: {attributes: []}
}]
}
],
attributes: [
'id',
'name',
[Sequelize.literal('COUNT("Employees"."id") OVER (PARTITION BY "CostCenters"."id")'), 'employeesCount']
]
});
I am trying to insert data in the last element of an array in MongoDB. insert in the first element works using the 0 operator, but I'm confused about how to insert in the last element. To explain it, consider the following data:
[
{
"_id": "1",
"company": "turivius",
"forms": [
{
"current_version": 1,
"history": [
{
"content": [],
"layout": [],
"responses": [
{
"client_id": 100,
"response_date": datetime(2022, 5, 17, 3, 0),
"values": [
{
"field_id": 0,
"primeiro user": "primeiro form, resposta 1",
},
{
"field_id": 1,
"value": "aaa",
},
],
},
{
"client_id": 100,
"response_date": datetime(2022, 5, 17, 3, 0),
"values": [
{
"field_id": 0,
"primeiro user": "primeiro form, resposta 2",
},
{
"field_id": 1,
"value": "bbb",
},
],
},
],
"version": 0,
},
{
"content": [],
"layout": [],
"responses": [
{
"client_id": 100,
"response_date": datetime(2022, 5, 17, 3, 0),
"values": [
{
"field_id": 0,
"primeiro user": "primeiro form, resposta 1 v1",
},
{
"field_id": 1,
"value": "aaa",
},
],
}
],
"version": 1,
},
],
"id": "33b66684-24a9-4a45-a12f-27a330152ac8",
"is_active": True,
},
{
"current_version": 0,
"history": [],
"id": "fa2eb9a1-c7c4-4b64-b682-7f51658bc4ab",
"is_active": False,
},
],
"user_id": "1",
},
{
"_id": "2",
"company": "turivius",
"forms": [
{
"current_version": 0,
"history": [],
"id": "565656",
"is_active": True,
},
],
"user_id": "9999",
},
]
I want to insert data in the document with user_id = 1, form.id = 33b66684-24a9-4a45-a12f-27a330152ac8 and in the history array where the version = 1 I want to insert a new response:
{
"client_id": 100,
"response_date": datetime(2022, 5, 17, 3, 0),
"values": [
{
"field_id": 0,
"test": "test",
},
{
"field_id": 1,
"test2": "test3",
},
],
}
The history with version 1 is the last element, if I wanted insert in the first element the following code would works:
find_one_and_update(
{"$and": [{"user_id": "1"}, {"forms.id": '33b66684-24a9-4a45-a12f-27a330152ac8'}]},
{
"$push": {"forms.$.history.0.responses": data_here},
},
return_document=ReturnDocument.AFTER,
)
Is there anyway to do it that way or I need a more complex query to do it?
You want to add a new document to the responses array when those conditions are true:
{
user_id: '1',
'forms.id': '33b66684-24a9-4a45-a12f-27a330152ac8',
'forms.history.version': 1
}
forms is an array, so you want to update the array element that matches the condition, so you can use the $[identifier] operator. Inside the array of forms, you only want to update the history with a specific version, so you need to use the $[identifier] operator multiple times, the field you want to update would be like this:
forms.$[f].history.$[h].responses
f is the identifier of each elements in forms that matches the condition and h the elements of the history array that match the other condition. An arrayFilters object is used to define the conditions.
The complete query could be like this:
db.collection.update({
user_id: "1",
'forms.id': '33b66684-24a9-4a45-a12f-27a330152ac8',
'forms.history.version': 1
},
{
$push: {
'forms.$[f].history.$[h].responses': {
"client_id": 100,
"response_date": "datetime(2022, 5, 17, 3, 0)",
"values": [
{
"field_id": 0,
"test": "test",
},
{
"field_id": 1,
"test2": "test3",
},
],
}
}
},
{
arrayFilters: [
{
'h.version': 1
},
{
'f.id': '33b66684-24a9-4a45-a12f-27a330152ac8'
}
]
})
Working example at MongoDB playground
Getting the error RangeError (end): Invalid value: Only valid value is 0: 1 when generating model when I try to generate model
Expected Behavior
when I use this command get generate model on match_details with assets/models/to_get/match_details.json
it should be creating a model inside the match_details folder with the fields inside the JSON file mentioned.
Current Behavior
When I try to do this I get the error RangeError (end): Invalid value: Only valid value is 0: 1 when generating model** when I try to generate model
Possible Solution
Am sure it is something about parsing the JSON file
More info
here is what the JSON file looks like
{
"success": true,
"details": {
"id": 22,
"date_time": "2022-04-12T20:00:00Z",
"home_team": {
"id": 69,
"home_strip": {
"id": 15,
"image": "/uploads/strip_5_0py3QXM.png"
},
"away_strip": {
"id": 16,
"image": "/uploads/WhatsApp_Image_2022-03-09_at_10.24.56_AM.jpeg"
},
"players": [{
"id": 47,
"is_cap": false,
"average_skill": 3,
"region": {
"id": 13,
"name_ar": "التجمع الخامس",
"name_en": "Fifth Settlement",
"state": {
"id": 8,
"name_ar": "القاهره",
"name_en": "Cairo"
}
},
"first_name": "Ezz El Din",
"last_name": "Karim",
"mobile": "01113481110",
"email": null,
"image": null,
"positions_ar": [{
"مدافع": 3
}],
"positions_en": [{
"CB": 3
}],
"basic_skills_ar": [{
"السرعة": 4
},
{
"تمرير الكرة": 3
},
{
"تسديد": 2
}
],
"basic_skills_en": [{
"speeding": 4
},
{
"Passing": 3
},
{
"shooting": 2
}
]
}],
"cap": {
"id": 46,
"is_cap": true,
"average_skill": 3,
"region": {
"id": 13,
"name_ar": "التجمع الخامس",
"name_en": "Fifth Settlement",
"state": {
"id": 8,
"name_ar": "القاهره",
"name_en": "Cairo"
}
},
"first_name": "Karim",
"last_name": "Garrana",
"mobile": "01113332257",
"email": "karim.garrana#gmail.com",
"image": null,
"positions_ar": [{
"مهاجم": 3
},
{
"مدافع": 4
}
],
"positions_en": [{
"ST": 3
},
{
"CB": 4
}
],
"basic_skills_ar": [{
"السرعة": 3
},
{
"تمرير الكرة": 3
},
{
"تسديد": 3
}
],
"basic_skills_en": [{
"speeding": 3
},
{
"Passing": 3
},
{
"shooting": 3
}
]
},
"average_skill": 3,
"points": 0,
"name": "كوبرا",
"league": 25,
"logo": "/uploads/9_IApeihR.png",
"region_ar": "التجمع الخامس",
"region_en": "Fifth Settlement"
},
"away_team": {
"id": 68,
"home_strip": {
"id": 14,
"image": "/uploads/strip_6.png"
},
"away_strip": {
"id": 13,
"image": "/uploads/strip_5.png"
},
"players": [{
"id": 46,
"is_cap": true,
"average_skill": 3,
"region": {
"id": 13,
"name_ar": "التجمع الخامس",
"name_en": "Fifth Settlement",
"state": {
"id": 8,
"name_ar": "القاهره",
"name_en": "Cairo"
}
},
"first_name": "Karim",
"last_name": "Garrana",
"mobile": "01113332257",
"email": "karim.garrana#gmail.com",
"image": null,
"positions_ar": [{
"مهاجم": 3
},
{
"مدافع": 4
}
],
"positions_en": [{
"ST": 3
},
{
"CB": 4
}
],
"basic_skills_ar": [{
"السرعة": 3
},
{
"تمرير الكرة": 3
},
{
"تسديد": 3
}
],
"basic_skills_en": [{
"speeding": 3
},
{
"Passing": 3
},
{
"shooting": 3
}
]
}],
"cap": {
"id": 45,
"is_cap": false,
"average_skill": 4,
"region": {
"id": 11,
"name_ar": "ميامى",
"name_en": "Miami",
"state": {
"id": 7,
"name_ar": "الإسكندرية",
"name_en": "Alexandria"
}
},
"first_name": "Ahmed",
"last_name": "Wagdi",
"mobile": "01207199086",
"email": "ahmed.w.amin#gmail.com",
"image": "/uploads/90f1e1c9-b430-4f9c-8347-a86be57f58676954964268664537546.jpg",
"positions_ar": [{
"مدافع": 5
}],
"positions_en": [{
"CB": 5
}],
"basic_skills_ar": [{
"تسديد": 5
},
{
"تمرير الكرة": 5
},
{
"السرعة": 5
}
],
"basic_skills_en": [{
"shooting": 5
},
{
"Passing": 5
},
{
"speeding": 5
}
]
},
"average_skill": 4,
"points": 2,
"name": "المنتقمون",
"league": 25,
"logo": "/uploads/6.png",
"region_ar": "التجمع الخامس",
"region_en": "Fifth Settlement"
},
"location": {
"id": 8,
"name_ar": "مدرسة الكابيتال",
"name_en": "Capital School",
"location": "30.014650515430546,31.4582347869873",
"region": 13
}
}
}
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 ?
I was doing a demo and could easily create and retrieve orders but was not able to get the expand parameter to work
For example
I created order SO003615 with:
URI http://localhost/Acumatica6/entity/Default/6.00.001/SalesOrder
{
"OrderType": { value: "SO" },
"CustomerID" : { value : "ACTIVESTAF" } ,
"LocationID" : { value : "MAIN" },
"Description" : { value : "Sample Order"},
"Details" :
[
{
"InventoryID" : {value: "AACOMPUT01"},
"Quantity" : {value: 2},
"UOM" : {value: "EA"},
"UnitPrice" : {value: 1000.99}
},
{
"InventoryID" : {value: "AALEGO500"},
"Quantity" : {value: 1}
}
]
}
Then I tried to get order with
URI http://localhost/Acumatica6/entity/Default/6.00.001/SalesOrder/SO/SO003615?expand=Details
But the line items are not in the result. What am I missing?
{
"id": "37c15980-f71d-4496-882d-6e05e4a50061",
"rowNumber": 1,
"note": "",
"BillingAddressOverride": {
"value": false
},
"BillingContactOverride": {
"value": false
},
"CreditHold": {
"value": false
},
"Currency": {
"value": "USD"
},
"CustomerID": {
"value": "ACTIVESTAF"
},
"CustomerOrder": {},
"Date": {
"value": "2017-03-16T00:00:00-04:00"
},
"Description": {
"value": "Sample Order 6"
},
"DestinationWarehouseID": {},
"ExternalReference": {},
"Hold": {
"value": false
},
"IsTaxValid": {
"value": false
},
"LastModified": {
"value": "2017-03-17T01:05:56.74-04:00"
},
"LocationID": {
"value": "MAIN"
},
"NewCard": {
"value": false
},
"OrderedQty": {
"value": 3
},
"OrderNbr": {
"value": "SO003615"
},
"OrderTotal": {
"value": 2101.98
},
"OrderType": {
"value": "SO"
},
"PaymentCardIdentifier": {},
"PaymentMethod": {
"value": "CHECK"
},
"PaymentRef": {},
"PreferredWarehouseID": {},
"Project": {
"value": "X"
},
"RequestedOn": {
"value": "2017-03-16T00:00:00-04:00"
},
"ShippingAddressOverride": {
"value": false
},
"ShippingContactOverride": {
"value": false
},
"ShipVia": {},
"Status": {
"value": "Open"
},
"TaxTotal": {
"value": 0
},
"custom": {},
"files": []
}
It is $expand, not expand. Try using the following
http://localhost/Acumatica6/entity/Default/6.00.001/SalesOrder/SO/003615?$expand=Details