Drupal how to use a sql case statement alike - drupal-8

I need to update all the nodes in the database based on a certain condition, and I need this update to be conditional. If a certain field is equal to a value, what the modified value will be X If it is otherwise, the value will be y, in a more accurate sense, I need something similar to the case statement in the sql.
Update [node]
Set [field] = CASE WHEN [field] IN (value1, value2, ...) THEN X ELSE Y END
WHERE [field] = value

I used the ternary operator to choose between the two values ​​to set and use the set function on the node object

Related

Query Drupal 8 content types with expression

In short, I need to do an entity query while running an expression. I can't find any way to accomplish this. I'm assuming there should be two ways.
An entity query with an expression - When I try this I can't get any expressions to work
a raw DB query, but I'm not familiar with how to join all the fields related to the content type of nodes I need.
Here is a shorthand example of what I need to accomplish
X = content type
y = field on x content type
z = field on x content type
My expression below is just an example, but need to run this in the database query
- Select y and z from x
- if x > y return node id
Any help would be great. This is going to run on a very large dataset, trying to find the fastest way to do the query in the database.
Comparing two fields is not possible using the entity query.
To do this, you may need to use the more low level Dynamic Queries and its where method:
$query = \Drupal::database()->select('node_field_data', 'n');
$query->condition('n.type', 'x'); // to get content type x
$query->innerJoin('node__field_y', 'y', 'y.entity_id = n.nid'); // join with field y
$query->innerJoin('node__field_z', 'z', 'z.entity_id = n.nid'); // join with field z
$query->where('z.field_z_value > y.field_y_value'); // your condition
$query->addField('n', 'nid'); // get node id in result
$results = $query->execute()->fetchAllKeyed(0, 0);

How to filter dates based on its time in Loopback 4?

I want to find out all the dates which have time more than 10.30 am.
let whereBuilder = new WhereBuilder();
whereBuilder.gt('sessionStartedOn', '10:30:00');
Obviously, this doesn't work. Are there any wildcard characters I should be adding?
The query in PostgreSQL would be something like this -
SELECT * FROM table WHERE date_part('hour', sessionStartedOn) >= 10 AND date_part('minutes', sessionStartedOn) > 30;
I think the second parameter should be of the same type as the value contained by the variable named by the first parameter. In your case I presume it is date. For example: "2022-01-18T00:00:00.000Z"
https://loopback.io/doc/en/lb4/apidocs.filter.wherebuilder.lte.html
PS. If you want to filter for values greater than the value passed as a parameter you should use .gt (>) or .gte (>=) methods.

Having trouble with IF NOT statments work in Sheets

this might be an easy fix but I just can't figure it out. I'm trying to get this IF statement, if the cell is not FALSE to return value, else to return a certain string. I've tried a couple of ways but I can't make the right combination. And I have a similar issue with excluding the FALSE value from a UNIQUE search statement.
This is the sample. Sorry if I'm missing on smth very obvious
Regarding your concern in setting a value if the cell is NOT FALSE, you can use this formula in Row 2:
=arrayformula(if(A2:A<>"",if(A2:A=FALSE,"Blank",iferror(year(to_date(datevalue(A2:A))),"No year found")),""))
What it does?
Check if cell value is FALSE, If yes, set cell value to "Blank", else convert the date string to value using datevalue(). Then use to_date() to convert date value into a date object. Use year() to get the year. Use iferror() to set a default value if the formula encountered an error (when your string is not a valid date string)
Loop each row using arrayformula()
Output:
Regarding filtering your data without FALSE:
=filter(A2:A,A2:A <> FALSE)
What it does?
Using filter(), filter the data if the cell value is not FALSE
Output:
Note:
You can also use UNIQUE() once you filter your data
=unique(filter(A2:A,A2:A <> FALSE))

Google Data Studio Calculated Field by Extracting String from Event Label Values

I'm trying to use the CASE statement to output string values for an Event Label field using RegEx to produce a table that shows the number of events for each field value. So, if I'm looking for foobar, and other string values separately, within values for Event Label; it may either stand alone or be part of a URL like so:
|[object HTMLLabelElement] | Foobar |
/images/foobar-26.svg
It seems REGEXP_EXTRACT might suit this the best:
CASE WHEN REGEXP_EXTRACT(Event Label, '.(?i)foobar.') THEN Foobar
However, the table produced using the calculated field as the dimension only contains a blank row that seems to be the sum of the number of events.
What am I missing?
I think you need to use REGEXP_MATCH not REGEXP_EXTRACT, given your existing syntax, or to change the syntax to a straight REGEXP_EXTRACT without the CASE element.

MongoDB: cannot upsert if query object and update object contain a same property , 'Cannot apply $addToSet modifier to non-array'

I want to execute a complex upsert, made by the following elements:
query on a field f by regex
update of the same field f (that is specified in the query object too)
Like this:
db.coll.update({ prop: /valueprop/i } , { $addToSet:{ prop:'a value' } } , true, false)
My expectation would be that if the document does not exist in the collection it is inserted with prop field equals to 'a value'; in other words it should insert a document with value of prop field that is the one specified in the update object.
Instead it throws Cannot apply $addToSet modifier to non-array , like if it tries to update the field prop with the value specified in the query object (that is a RegExp object) instead of using the value specified in the update object.
Isn't it a bug?
The workaround is using $all keyword in the query object in the following way
db.cancellami.update({prop:{$in:[/regex_value/i]}},{ $addToSet:{prop:'a value'}} ,true,false)
As said in the documentation $addToSet operator adds a value into an array only if the value is not in the array already: http://docs.mongodb.org/manual/reference/operator/update/addToSet/#up._S_addToSet
Your prop value is an string so you couldn't use $addToSet. Instead of this you could do the next:
db.collection.update({prop:/valueprop/i},{$set:{prop:['a value']}}, true, false)
Update:
In case you need to ensure that are not going to exist duplicates in your arrays, you must first update all your documents to convert them into arrays with something like this:
db.collection.find({ $where : "isString(this.prop)"}).forEach(function (doc) {
doc.prop = [doc.prop];
db.collection.save(doc);
})
And then you will be able to execute the next to ensure you'll always update only array fields and no errors will be found:
db.collection.update({ $where : "Array.isArray(this.prop)"},{$addToSet:{prop:'a value'}}, true, false)