incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tim Stewart <finelinestu...@gmail.com>
Subject Sort "join" view
Date Sat, 16 Nov 2013 03:09:37 GMT
I'm learning about CouchDB, and wondering how I might solve the following
problem. Given docs of two types (category and item) how would I produce a
view grouping items with their category, ordered by name. It's that last
bit about ordering by name that's stumping me. I can achieve the grouping,
and indeed ordering of items by name within each category, as follows:

    function map(doc) {
      if (doc.type === "category") emit([doc._id, 0], doc);
      else if (doc.type === "item") emit ([doc.category_id, 1, doc.name],
doc);
    }

But the categories are sorted in ID order, not by name.

I thought about using category name in its ID, so it would use that for
sorting, but that would mean updating ID on the category and updating all
its items every time the name changed.

I thought about modelling the relationship differently with items nested
within each category, but in this case an item can belong to multiple
categories so it's not a good match.

I thought about approaches based on filtered replication or combining views
(is that even possible) but couldn't see a solution.

Is there a simple thing I'm missing, or is the only solution to do this
within the consuming app? Hoping to avoid that so I can use pagination etc.
via the DB view.

Thanks
Tim

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message