himekami
himekami

Reputation: 1459

Querying Couchbase using N1QL

I can query docs using views just fine, but switching to N1QL gives me Success property as false. What went wrong ?

    let cluster = new Cluster()
    let bucket = cluster.OpenBucket("mydoc","")
    let query = """SELECT * FROM mydoc where SET = 'SET24MM2SCLV01'"""
    let result = bucket.Query(query)
    Console.WriteLine(result.Success) //would give false

Upvotes: 4

Views: 1662

Answers (1)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131714

SET is a reserved word in N1QL. In order to use it as an identifier, you need to escape it with backticks, eg:

SELECT * FROM mydoc where `SET` = 'SET24MM2SCLV01'

If you don't you'll get a syntax error:

"errors": [
    {
        "code": 3000,
        "msg": "syntax error - at SET"
    }
]

You should change your query to

let query = """SELECT * FROM mydoc where SET = 'SET24MM2SCLV01'"""

let query = "SELECT * FROM mydoc where `SET` = 'SET24MM2SCLV01'"

EDIT

The query result also contains an Error property with the errors that occured in the query. This should be checked always if Success returns false. If the query fails even after escaping SET, this will explain what other error prevents the query from running.

For example, I just noticed that the entire query is enclosed in double quotes. This would send a string literal to the server instead of a query.

Upvotes: 6

Related Questions