nhtrung
nhtrung

Reputation: 55

how to use LIKE n1ql query in couchbase?

I am want to represent a matter that i haven't understand yet . I show you a following example . This is my documents

doc 1 : {"type":"81"}
doc 2 : {"type":"81_"}
doc 3 : {"type":"8"}
doc 4 : {"type":"8_"}
doc 5 : {"type":"13"}

And now, I want to query using n1ql to fetch documents that match type like "8_%" 8_ should be a String prefix

@Query("#{#n1ql.selectEntity} where code like "8_%" ") 
**or**
@Query("#{#n1ql.selectEntity} where code like (concat(8,_,'%')) ")

Both of them not correct anwser that i am except.Response should only have one doc is doc 4 : {"type":"8_"} . But this response following documents .

 doc 1 : {"type":"81"}
 doc 2 : {"type":"81_"}
 doc 3 : {"type":"8"}
 doc 4 : {"type":"8_"}

Can you help me in situation ? Thank you in advanced .I am using Couchbase 3.1.8 . Sorry my bad english

Upvotes: 2

Views: 4018

Answers (1)

vsr
vsr

Reputation: 7414

The following query should return right results

SELECT META().id FROM mybucket WHERE type LIKE "8_%"

Match string with a wildcard expression. Use % for zero or more wildcards and _ to match any character at this place in a string ( https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/comparisonops.html)

If you want match _ as it is escape it.

SELECT META().id FROM mybucket WHERE type LIKE "8\\_%";

If you are using Couchbase 3.1.8, it is very old and you should try latest Couchbase version 6.5.0.

I think the N1QL release is started in Couchbase 4.0.0

Upvotes: 1

Related Questions