0rka
0rka

Reputation: 2386

How to query documents which are arrays

I'm a novice Couchbase user and I have a bucket which I've created that contains documents which are actually arrays in the form of:

{
    "key": [
        {
            "data1": "somedata1"
        },
        {
            "data2": "somedata2"
        }
    ]
}

I want to query these documents via N1QL statements and have yet to find a solution to how to do this properly. More specifically, I would like to select fields inside each sub-document that is in an array of a certain key. For example, I would like to access: key.[1].data2 or key.[0].data1

How should I do it?

Upvotes: 1

Views: 43

Answers (1)

Hod
Hod

Reputation: 2276

Couchbase has some reserved keywords that need to be escaped. In this case, key needs to be escaped. For example, if you're querying against my_bucket, then

SELECT my_bucket.`key`[0].data1 FROM my_bucket;

should return somedata1

Upvotes: 2

Related Questions