Vladimir Sidorenko
Vladimir Sidorenko

Reputation: 4265

Select documents that don't have some keys in CouchDB

Having an article document with an embedded array of tags, how to select articles that don't contain some specific tags?

Thanks

Upvotes: 3

Views: 602

Answers (2)

alesch
alesch

Reputation: 842

Yes, as @AviFlax suggests, using a _list to post process your view will work.
Note that you can send arbitrary parameters to the view, and get them in the list inisde the req.query object.

Example:

GET http://host/db/_design/ddoc/_list/a-list/a-view?some="thing"

The arbitrary parameter some, can be obtained within the list function, and thus used for filtering or other processing.

function(head, req) {
   var some = req.query.some
   ....
}

Upvotes: 0

Avi Flax
Avi Flax

Reputation: 51819

It's pretty easy to create views which exclude documents which don't contain some specific tags. This is documented here: CouchDB Wiki: View Snippets: Retrieving documents without a certain field.

However, I suspect what you really want is a way to do this dynamically, by having a single view to which you could pass a parameter containing the tag(s) you wanted to exclude. That's pretty tricky, and I don't know how one would do it. If that's the case, I suggest you post a question to the couchdb-user mailing list or to the IRC channel (irc://irc.freenode.net/couchdb). It's an active and helpful community so someone will definitely try to help you figure this out.

FWIW, I did a quick search and found this interesting idea: Using _list for query post-processing. There were no responses, but it's an interesting idea and possibly worth exploring.

HTH!

Upvotes: 2

Related Questions