How to use 'IN' opeartor in Spring boot & Couch base -N1QL query - spring

I have a JSON list from which from which I 'm looking up for a particular value using N1QL query.
It works when I try only if there is only one value in the list.
But if there are multiple values in the list I'm not getting in response.
Json
"suppliers_list": [
"767",
"300767"
]
#Query("SELECT users.*, "
+ "META(users).id as _ID, META(users).cas as _CAS FROM #{#n1ql.bucket}"
+ "users WHERE lower(type)='users' AND ANY usersupplist IN users.suppliers_list SATISFIES usersupplist IN [$1] END")
userRepository.findBySupplierList(String List)
usersDefObjList = userRepository.findBySupplierList("767"); //Working
usersDefObjList = userRepository.findBySupplierList("767,300767"); // Not Working
usersDefObjList = userRepository.findBySupplierList("'767','300767'"); // Not Working
But the same one like works in CouchBase Query window
SELECT users.*,META(users).id as _ID,META(users).cas as _CAS FROM TCI_DATA_MIG users WHERE lower(type)='users' and ANY usersupplist IN users.suppliers_list SATISFIES usersupplist IN ['767','300767'] END
Please let me know your suggestions to get the values in the repository N1QL query to fix this.

In the N1QL right side of IN clause requires ARRAY.
If you know how many elements you can use
usersupplist IN [$1,$2,$3]
If the ARRAY is dynamic and don't know how many elements you can use following and pass in as ARRAY.
usersupplist IN $1
$1 = [ "767", "300767" ] i.e. JsonArray.from("767", "300767")
Check this out Couchbase parameterized N1QL query IN statement

Related

Null and Empty Check for a IN Clause parameter for Spring data jpa #query?

My Spring data JPA code to get the data from db based on some search criteria is not working. My DB is SQL Server 2012, same query seem to work with MYSQL DB.
Code Example :
#Query(value = "select * from entity e where e.emp_id=:#{#mySearchCriteria.empId} and ((:#{#mySearchCriteria.deptIds} is null or :#{#mySearchCriteria.deptIds} ='') or e.dept_id in (:#{#mySearchCriteria.deptIds})) ", nativeQuery = true)
public List<Entity> search(#Param("mySearchCriteria") MySearchCriteria mySearchCriteria);
if list mySearchCriteria.deptIds has more than one value- it's not working(it's actually translating it to wrong query. Any lead? Thanks in advance.
Note: data type for deptIds is List of Integer
Its complaining because values of {#mySearchCriteria.deptIds} is comma separated list e.g. 'Value1', 'Value2' so the query gets translated as ('Value1', 'Value2' is null) which causes this error.
Need to verify if list is empty or not and then change the query with IN clause and one without IN clause.
Surround the list by parentheses. This works for me.
(:#{#mySearchCriteria.deptIds}) is null

Elasticsearch query not returning expected results for multiple should filters

I am performing an Elasticsearch query using the high-level-rest-api for Java and expect to see records that are either active or do not have a reference id. I'm querying by name for the records and if I hit the index directly with /_search?q=, I see the results I want.
Is my logic correct (pseudo-code):
postFilters.MUST {
Should {
MustNotExist {referenceId}
Must {status = Active}
}
Should {
MustNotExist {referenceId}
Must {type = Person}
}
}
What I get are records that are active with a reference id. But, I want to include records that also do not have a referenceId, hence why I have MustNotExist {referenceId}.
For simplicity, the second Should clause can be dropped (for testing) as the first one is not working as expected by itself.
In my case, I had to use a match query instead of a term query because the value I was querying for was not a primitive or a String. For example, the part where Must, type = Person, Person was an enum, and so looking for "Person" was not quite right, whereas match allowed it to "match".

MongoRepository Compared two column + Spring Data

I would like to execute the query with compared two column value using mongorepository + spring data (HQL or criteria query). I am showing you relation query and i want to convert to mongo.
Select * from Employee em where em.limit < em.age
I want to convert above query to HQL query using Spring data.
You can use $where to achive this:
db.employee.find( { $where: "this. limit < this. age" } );
check out the document here:
https://docs.mongodb.com/manual/reference/operator/query/where/

removing backslashes in the query using codeigniter mysql

Post value is multi select, means values are coming in the form of array
if(!empty($_POST['form_type']))
{
$test = implode("','",$_POST['form_type']);
$this->db->where_in('enquiry.type_of_enquiry',"'".$test."'");
}
my query is like this
SELECT `sobha_enquiry`.`name`, `sobha_enquiry`.`date_created`, `sobha_enquiry`.`company`, `sobha_enquiry`.`form_of`, `sobha_enquiry`.`projectname`, `sobha_enquiry`.`city`, `sobha_enquiry`.`country`, `sobha_enquiry`.`phone`, `sobha_enquiry`.`type_of_enquiry`, `sobha_enquiryzone`.`enquiry_id`, `sobha_enquiry`.`hearaboutus`, `sobha_enquiry`.`email`, `sobha_enquiry`.`comments`, `sobha_enquiry`.`address`, `sobha_admin`.`id`, `sobha_admin`.`city_id` FROM (`sobha_senquiry`) LEFT JOIN `sobha_enquiryzone` ON `sobha_enquiryzone`.`enquiry_id` =`sobha_enquiry`.`id` LEFT JOIN `sobha_admin` ON `sobha_admin`.`city_id`=`sobha_enquiryzone`.`city_id` WHERE `sobha_enquiry`.`type_of_enquiry` IN ('\'register form\',\'feedback form\'') GROUP BY `sobha_enquiry`.`id` ORDER BY `sobha_enquiry`.`id` desc LIMIT 15
since i used implode function it is coming like this IN ('\'register form\',\'feedback form\'') i want to remove those backslashes . Please help me
$this->db->where_in() will accept array as second argument.
So there is no need for imploding the data.
if(!empty($_POST['form_type']))
{
$this->db->where_in('enquiry.type_of_enquiry',$_POST['form_type']);
}
This will result as,
WHERE `enquiry`.`type_of_enquiry` IN ('register form','feedback form')
You can refer this link for more info, Codeigniter Active Record Documentation

Linq To NHibernate: .StartsWith on multiple properties

I'm trying to accomplish the following query (notice .StartsWith):
return (from p in _session.Linq<Profile>()
where (p.Firstname + " " + p.Lastname).StartsWith(wildcard)
select p).ToList();
This throws: could not resolve property: Firstname.Lastname.
If I do this:
return (from p in _session.Linq<Profile>()
where p.Firstname.StartsWith(wildcard)
select p).ToList();
Everything is working. How can this be?
Thanks in advance!
The Where Expression does not know how to handle the concatenation of strings. It is trying to make sense of the properties, not the values.
Also, for future reference the StartsWith with the concat and the other one with out would in practice return the same thing.
Is this what you want?
return (from p in _session.Linq<Profile>()
where p.Firstname.StartsWith(wildcard) || p.Lastname.StartsWith(wildcard)
select p).ToList();
Update: rewritten answer based on new insights and edited questions.
What's in wildcard and what's the expected output vs. input? If you concat "Abel" + " " + "Braaksma" it will return true for wildcard.StartsWith("Abel") or wildcard.StartsWith("Abel Br") but not wildcard.StartsWith("Braaks"). Do you perhaps mean Contains instead? But this won't solve your error:
The exception you receive seems to come from NHibernate, not from your code. Is it possible that Lastname does not have a correct mapping to the database table? Can you show the stacktrace? Can you access the properties outside of the context of the LINQ statement, but filled with data from the table?

Resources