Couchbase N1QL Json Array String Wild Card Search - json

Is is possible to wild card search JSON String Array in a way similar to %Mr.% in N1QL . I know that exact value could be queried like 'Jon' in names where names is the JSON array
{
"use" : "official"
"names" : ["Jon", "Snow", "Smith", "Mr. Smith"]
}
But I want to search like %Mr.% in names in N1QL . Is it possible ? I have tried but it failed to do so , I have also tried Regex functions but seems like it only works for key value pair not for Array Searching.

Try this.
WHERE ANY name IN names SATISFIES name LIKE '%Mr.%' END;
Also see
https://dzone.com/articles/a-couchbase-index-technique-for-like-predicates-wi
https://dzone.com/articles/more-than-like-efficient-json-search-with-couchbas
https://dzone.com/articles/split-and-conquer-efficient-string-search-with-n1q

Related

How to Take Just Data Without Column Names in NodeJS with MySql

When I want to take data from mysql in NodeJS, it results like
RowDataPacket { name : 'John', marks : 56}
But I want to take this data without column names or other things. I need to use just NodeJS for this problem. I saw some JSON solutions but I can’t do this cause of my program.
The result of a 'SELECT' query will be an array of objects. And each object defines a row.
You can either iterate through the array and use the key to access its value like results.forEach( row => { row[key] })
or if your result returns a single row, then you can access the object as results[0].
You need to set an index and then obtain the value.
For example if you are storing your response in a variable called results.
Use the following:
x = results[0].name;
Then you can use this variable/name further in your program.

Extract certain members of JSON array in MySQL

I have a table in MySQL where each row contains JSON returned from another system. The JSON will look something like:
[{"userId": "Dave"},{"userId": "Mary", "errorCode" : "DB Fail"}, {"userId": "Lorenza", "errorCode": "Web Error"}]
and I'm only interested in the members of the array containing an error code. In the future, these will be parsed into seperate rows of their own table, but in the meantime does MySql offer a way to extract only these with an errorCode?
I can use JSON_EXTRACT to extract the errorCodes only
JSON_EXTRACT(jsonData, '$[*].errorCode') AS errorCodes
but I really want the rest of the member (userId in the example above)
You could use the JSON_CONTAINS function to find the records with errorCode and then then use JSON_EXTRACT on those records. Put the JSON_CONTAINS in the where clause
I don't think you could do this with a single query without known boundaries of the number of elements, but you could use a stored procedure to run a loop.
e.g. each iteration runs LOCATE to find the position of "errorCode", and uses that location to run SUBSTR and/or SUBSTRING_INDEX to get the userid value and append it to another variable. The looped variable would just be the offset used in the LOCATE query.

Convert LIST of JSONs to a list of strings using postgresql

I have this of JSON object :
[{"name":"steven","dob":"1979-02-10"},{"name":"Chris","dob":"1981-01-05"},]
I want to convert the object above to a LIST of the names from the JSON objects like :
["steven", "Chris"]
Is there's a postgresql function to do that?
I checked the documentation HERE and could not find the right function
Try this:
SELECT json_agg(value->>'name') FROM json_array_elements('[{"name":"steven","dob":"1979-02-10"},{"name":"Chris","dob":"1981-01-05"}]');

Querying a MySQL table with a JSON field and accessing JSON attributes

With MySQL 5.7 new features involving JSON has emerged. Among these features is the ability to query the fields in the JSON object as it is stored in the database.
My object looks like this.
{
"color": [
{"WHITE" :{ "size": [
{"S": [{"Price" : "31"},
{"discountPrice" : "13" }]}
]}},
{"BLACK" :{ "size": [
{"S": "69"},
{"M": "31"},
{"L": "55.666"}
]}}
]}
I want to query this as if it was regular tabular data, to this end I tried the following query to no avail.
select json_extract(Sku, '$.color[0]') from CRAWL.DAILYDATA;
I want to explode this into a format that looks more like a traditional RDBMS.
Any ideas?
In order to get data out of a json object as values, you need to get all the way down to the values. For instance, if you wanted to pull all of the values like they are regular RDBMS columns:
select json_extract(Sku, '$.color[0].WHITE.size[0].S[0].price') as price,
json_extract(Sku, '$.color[0].WHITE.size[0].S[0].discountPrice') as discountPrice
from CRAWL.DAILYDATA;
Of course, you need to know exactly what you're looking for in the object. This is the price of having a schema-less object like json. In principle, you could define a mysql function that would use combinations of
json_contains_path
and
json_extract
to make sure the path you are looking for exists, and otherwise it returns null. Personally though, if you want the RDBMS quality, why not just force it into a form where you can put the values directly into mysql tables? This is, of course, why RDBMS's exist. If you can't put it into such a form, you're going to be stuck with searching your json as above.

Dynamic field name query using N1QL

I'm having a use case here which I can't seem to solve. Basically, I need to create a webservice where users may query the couchbase cluster "dynamically". Indeed, i'm storing metadata of different files, and the "creation" of this metadata is up to the user : I don't have specific fields in my Java POJO, i'm inserting a MAP which gets inserted as a nested object in couchbase.
Now the query I need is pretty simple on paper and looks something like this :
#Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND $1 = $2")
List<FileMetadata> findListMetadata(String pKey, String pValue);
But it doesn't seem to work, $1 doesn't seem to ever get replaced by the pKey variable.
I'm using CouchBase 4.5 with the Spring Data connector.
Any ideas on how to solve that use case ?
You need to dynamically generate the query string, so that pKey is inserted into the query string and pValue is passed as a parameter (as you are doing).