How to update JSON node that matches criteria based on attribute value (instead of index)? - postgresql

Postgresql 10+
Example from the documentation...
jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]', false)
results in...
[{"f1":[2,3,4],"f2":null},2,null,3]
Fair enough. But I need to find my target node by attribute value, not index. For the life of me, I cannot figure out how do something like...
jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{(where f1 = 1),f1}','[2,3,4]', false)
Any advice on how to accomplish this?
Thanks!

You can split the steps into two jobs:
Split in elements (jsonb_arral_elements)
Indentify wich elements must change (case when...)
Update that element (jsonb_set)
Join all together (jsonb_agg)
solution
select jsonb_agg(case when element->>'f1'='1' then jsonb_set(element, '{f1}', '[2,3,4]') else element end)
from jsonb_array_elements('[{"f1":1,"f2":null},2,null,3,{"f1":3},{"f1":1,"f2":2}]'::jsonb) element
note
I changed the input adding two more elements with "f1" key

Related

string match and remove each row in a postgresql

So in short:
I need to find all rows in the column Translation that begin with the letter M (M123 is one of the Prefixes) and then I need to remove the M123 and similar prefixes from all the rows.
For example, row 1 contains the following data:
M123 - This is translated from Spanish to English
And I need to remove the M123 - from the mentioned data. And this I need to do for the Translation column in every row in the table.
It's been a while since I actually did some SQL-Queries. So I tried a WHERE clause to find all the M prefixes but my query returns an empty search. Following is the query I am using atm:
SELECT Translation from Translation_Table where Translation like 'M';
I am a little bit confused right now. So any help is appreciated.
I sense that you might be wanting to do an update here, rather than a select:
UPDATE Translation_Table
SET Translation = REGEXP_REPLACE(Translation, 'M[0-9]+', '')
WHERE Translation ~ '^M[0-9]+';
addition to this answer following query will remove all occurence of M[any length of a number]
UPDATE Translation_Table
SET Translation = REGEXP_REPLACE(Translation, '[M[:digit:]]', '', 'g')
WHERE Translation ~ '.M[0-9]*';

How can I achieve IF ALL ELSE, in Decision Table?

Drools 5.4
I have a decision table that sets values in the BlueReport object based on Channel Name attribute. Everything works, except if a Channel name contains an unknown channel for which we don't have mappings, we need to set the values to indicate this condition. The picture below should illustrate this more clearly, I'm sure. Here is what we want:
How can we achieve the "ALL OTHERS" default condition?!
I've evolved my spreadsheet rules to this now:
In this version of DT above, I have left B15 blank, and then I have added a new condition on C15 which checks the auditRule field for presence of string variable "DIV". I don't know if I have the right syntax for it in C9?! The ruleAudit field is update everytime there is a match for Channel Name (F11 - F15). Therefore, absence of DIV rule name, would indicate that there is no match to any of the patterns on B11 - B14. What do you think?!
Add a new condition column again with declaration for blueReport and set this condition
channel_name!=$1 || channel_name!=$2 ||channel_name!=$3 || channel_name!=$4
New Condition's value field must be empty for the first four records except these values like ChannelName1,ChannelName2,ChannelName3,ChannelName4 in "ALL OTHERS" row. Though this isn't the standard solution try this workaround to check
I think the post can help you. It means you should not only declare sequential in the RuleSet, but also declare PRIORITY in RuleTable that the rules will be called according to the value of PRIORITY.

Scala: filter Seq[] (make a diff)

I have two Seq[_]es in my Play application.
Now I want to make a diff of those and get as a result an Seq with all items which are not in the other one.
I tried to use .filter() but I don't know if thats a good way
How can I achieve this?
thanks in advance
Update ... PseudoCode Example
I have two Seq[]
1.) Seq[CarsInStock]
Attributes[ID, Brand, Color]
[{1,Porsche,Red},{3,Mercedes,Blue}]
2.) Seq[CarsAfterSale]
Attributes[ID, Brand, Color,Doors,Windows]
[{1,Porsche,Red,4,10}]
Now I wan't to make a diff between the two seq[]. As result I want to get the Object {3,Mercedes,Blue}] because it is in stock, but after sales I have to know which ones I have to remove from stock.
I want to recognize the difference by the ID of the elements
You can simply filter out all cars whose id exist in the other Seq.
stock.filterNot(c => afterSale.exists(_.id == c.id))
Unless you expect the second Seq to be short, you can probably optimize it by creating a Set of ids:
val afterSaleIds = afterSale.iterator.map(_.id).toSet
stock.filterNot(c => afterSaleIds.contains(c.id))

AEM CQ5 Query Builder: How to get result by searching for 2 different values in same property?

I want to get result matches with all nodes contains property 'abc' value as 'xyz' or 'pqr'.
I am trying in below ways:
http://localhost:4502/bin/querybuilder.json?path=/content/campaigns/asd&property=abc&property.1_value=/%xyz/%&property.2_value=/%pqr/%property.operation=like&p.limit=-1&orderby:path
http://localhost:4502/bin/querybuilder.json?path=/content/campaigns/asd&property=abc&property.1_value=/%xyz/%&property.2_value=/%pqr/%&property.1_operation=like&property.2_operation=like&p.limit=-1&orderby:path
http://localhost:4502/bin/querybuilder.json?path=/content/campaigns/asd&1_property=abc&1_property.1_value=/%xyz/%&1_property.1_operation=like&2_property=abc&1_property.1_value=/%xyz/%&2_property.1_operation=like&p.limit=-1&orderby:path
But none of them served my purpose. Any thing that I am missing in this?
The query looks right and as such should work. However if it is just xyz or pqr you would like to match in the query, you may not need the / in the values.
For eg.
path=/content/campaigns/asd
path.self=true //In order to include the current path as well for searching
property=abc
property.1_value=%xyz%
property.2_value=%abc%
property.operation=like
p.limit=-1
Possible things which you can check
Check if the path that you are trying to search contains the desired nodes/properties.
Check if the property name that you are using is right.
If you want to match exact values, you can avoid using the like operator and remove the wild cards from the values.
You can actually use the 'OR' operator in your query to combine two or more values of a property.
For example in the query debug interface : http:///libs/cq/search/content/querydebug.html
path=/content/campaigns/asd
property=PROPERTY1
property.1_value=VALUE1
property.2_value=VALUE2
property.operation=OR
p.limit=-1
It worked with below query:
http://localhost:4502/bin/querybuilder.json?orderby=path
&p.limit=-1
&path=/content/campaigns
&property=jcr:content/par/nodeName/xyz
&property.1_value=pqr
&property.2_value=%abc%
&property.operation=like
&type=cq:Page
Note: property name should be fully specified form the type of node we are expecting.
Ex: jcr:content/par/nodeName/xyz above instead of just xyz

JQuery Wildcard for using atttributes in selectors

I've research this topic extensibly and I'm asking as a last resort before assuming that there is no wildcard for what I want to do.
I need to pull up all the text input elements from the document and add it to an array. However, I only want to add the input elements that have an id.
I know you can use the \S* wildcard when using an id selector such as $(#\S*), however I can't use this because I need to filter the results by text type only as well, so I searching by attribute.
I currently have this:
values_inputs = $("input[type='text'][id^='a']");
This works how I want it to but it brings back only the text input elements that start with an 'a'. I want to get all the text input elements with an 'id' of anything.
I can't use:
values_inputs = $("input[type='text'][id^='']"); //or
values_inputs = $("input[type='text'][id^='*']"); //or
values_inputs = $("input[type='text'][id^='\\S*']"); //or
values_inputs = $("input[type='text'][id^=\\S*]");
//I either get no values returned or a syntax error for these
I guess I'm just looking for the equivalent of * in SQL for JQuery attribute selectors.
Is there no such thing, or am I just approaching this problem the wrong way?
Actually, it's quite simple:
var values_inputs = $("input[type=text][id]");
Your logic is a bit ambiguous. I believe you don't want elements with any id, but rather elements where id does not equal an empty string. Use this.
values_inputs = $("input[type='text']")
.filter(function() {
return this.id != '';
});
Try changing your selector to:
$("input[type='text'][id]")
I figured out another way to use wild cards very simply. This helped me a lot so I thought I'd share it.
You can use attribute wildcards in the selectors in the following way to emulate the use of '*'. Let's say you have dynamically generated form in which elements are created with the same naming convention except for dynamically changing digits representing the index:
id='part_x_name' //where x represents a digit
If you want to retrieve only the text input ones that have certain parts of the id name and element type you can do the following:
var inputs = $("input[type='text'][id^='part_'][id$='_name']");
and voila, it will retrieve all the text input elements that have "part_" in the beginning of the id string and "_name" at the end of the string. If you have something like
id='part_x_name_y' // again x and y representing digits
you could do:
var inputs = $("input[type='text'][id^='part_'][id*='_name_']"); //the *= operator means that it will retrieve this part of the string from anywhere where it appears in the string.
Depending on what the names of other id's are it may start to get a little trickier if other element id's have similar naming conventions in your document. You may have to get a little more creative in specifying your wildcards. In most common cases this will be enough to get what you need.