keeterDev
keeterDev

Reputation: 145

Couchdb/Cloudant view for document property with array

I have three docs in CouchDB/Cloudant as follows:

all_docs :[{
    _id: "content1"
    files: ["fileA", "fileB", "fileC"]
}, {
    _id: "content2"
    files: ["fileD", "fileE", "fileC"]
}, {
    _id: "content3"
    files: ["fileF", "fileG", "fileH"]
}

I need to get content1 and content2 as result for FileC where I can pass FileC as key.

Is there a way to write a view that returns doc ids (content1 and content2) where the file is FileC?

Upvotes: 0

Views: 89

Answers (1)

Jonathan Hall
Jonathan Hall

Reputation: 79764

This just requires a standard, straight-forward view.

As documented:

Note that emit() may be called many times for a single document, so the same document may be available by several different keys.

So you just need to call emit() for each file in the document. Something like this:

function (doc) {
  for (i = 0; i < doc.files.length; i++) {
    emit(doc.files[0])
  }
}

Upvotes: 4

Related Questions