6

I have some documents in couchdb that have fields that are arrays of id's for different associated documents:

{
  associatedAssets: ["4c67f6241f4a0efb7dc2abc24a004dfe",  "270fd4508a1222a1e2a27cbe7f002d9z"]
}

I would like to write a view that will let me pass in a key that is itself an array of ids, and then return documents whose associatedAssets fields contain one or more of the ids passed in via the key array e.g.

$.ajax({
  url: "/db/_design/design_doc/_view/summaryByAssociatedAssets",
  type: "post",
  data: JSON.stringify({keys: ["4c67f6241f4a0efb7dc2abc24a004dfe", "6c67f6241f4a0efb7dc2abc24a004dfd"]}),
  dataType: "json",
    contentType: "application/json",
})
.done(function(resp){
  console.log(resp[0]);
});

would return documents whose associatedAssets array contains one or more of the keys "4c67f6241f4a0efb7dc2abc24a004dfe", "6c67f6241f4a0efb7dc2abc24a004dfd".

I can't access the keys in my view, so I'm not sure if I can do this? Is there a better way to accomplish this?

Thanks!

2 Answers 2

11

Your view just needs to generate an output row per associatedAssets element, something like this:

function(doc) {
  if( doc.associatedAssets ) {
    for( var i=0, l=doc.associatedAssets.length; i<l; i++) {
      emit( doc.associatedAssets[i], doc );
    }
  }
}

Then you'd need to adjust your call so it ends up passing that keys array as a query string parameter, which will return only the rows from the view that match keys in that array.

A total aside - assuming a recent CouchDB version, the best practices would be to replace doc in your emit with { _id: doc._id } and then use include_docs=true in your query so your view index doesn't get filled up (unnecessarily) with full documents.

Sign up to request clarification or add additional context in comments.

2 Comments

Im doing the same but cannot get the query parameter for keys correct, any ideas how to make this work? Heres what works: ?keys=[["apex_fundamentals"]] but the following does not work: keys=["apex_fundamentals","javascript_and_visualforce"] (NOTE: the above is run through to_json and url encoded) and all in rails)
Based on what works and what doesn't I'd suggest that what you're emit()ing in your view is actually an array, not a string.
0

In the view code you can access anything in the document itself, but there is no way to access any parameters that you pass in.

You could use temporary views that are generated for the specific query you are doing.

You could also use ElasticSearch instead. ElasticSearch has a much richer query DSL because that's the whole point of ElasticSearch. The CouchDB River allows you to automatically index all documents from CouchDB.

Using ElasticSearch, you could just search for any document whose associatedAssets contains any element from a list that you pass in.

3 Comments

It is true that you cannot access any parameters, but by keying your output sensibly you can use the key or keys query string parameter built into CouchDB to restrict the outputted rows to only those that match those keys.
Thanks!! I was looking for that exact feature (the keys parameter) and thought it didn't exist.
No problem, here's a full list of the available query options.

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.