Query document with excluded key

Hi,

Is it possible to exclude a key in the query by just getting all the documents except the provided key? Example, I have a key “18”, all of the documents with the key “18” should not be included in the query result.

If this is possible, how am I going to accomplish this?

Kindest regards,
Robert

@robert

Based on your use of the word query, I’m going to assume you’re referring to N1QL queries.

Yes, it is possible to query all documents and then exclude given keys using the WHERE clause. However, this can have very poor performance so I’d be careful with your use case.

To do so, use the META function. This returns an object with various document properties, one of which is “id”. This property is the document key.

Here is an example query based on your example, assuming the bucket name is “default”:

SELECT * FROM default WHERE META(default).id <> '18'

If you are using Linq2Couchbase, it can be represented as:

bucketContext.Query<DocType>().Where(p => N1QlFunctions.Key(p) != "18");