0

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?

1 Answer 1

4

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])
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Flimzy!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.