Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Couchbase or N1QL how can I check if the values in an array match

In a couchbase I have the following document structure...

{
    name: "bob",
    permissions: [
        2,
        4,
        6
    ]
}

I need to be able to create a view, or N1QL query which will check if the permissions for "bob" are contained within a given array.

e.g I have an array with contents

[1,2,3,4,5,6]

I need the "bob" document to be returned because my array contains 2,4,6 and so does "bob"

If my array contained 1,3,4,5,6 "bob" should not be selected because my array does not contain "2"

Essentially I want to match any documents whose permission entries are all contained in my array.

The solution can either a view or an N1QL query.

like image 224
Matthew Avatar asked Mar 04 '15 18:03

Matthew


1 Answers

Using N1QL, you can do the following:

SELECT * FROM my_bucket WHERE EVERY p IN permissions SATISFIES p IN [ 1,2,3,4,5,6 ] END;
like image 136
geraldss Avatar answered Oct 15 '22 23:10

geraldss