couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dean Landolt <d...@deanlandolt.com>
Subject Re: couchdb as backend for a CMS
Date Thu, 29 Jan 2009 17:15:41 GMT
On Thu, Jan 29, 2009 at 10:35 AM, Gaspard Buma <gaspard.buma@gmail.com>wrote:

> But the example (comments in posts) is a "belongs_to" relation (no
> need for an external join table). Is there any example with a "has
> many and belongs to many" example ?
>
> My feeling is that a habtm relation could be modeled with something like:
>
> {
>  "name": "John",
>  "friends": ["Paul", "Mike", "Carla"]
> }
>
> Then you index on friends:
>
> function(doc) {
>  doc.friends.map(function(friend) {
>    emit(friend, doc);
>  });
> }
>
> That's fine (except you have to update "John" and "Carla" if they are
> not friends anymore). But what if you move one level further and need
> to see the latest posts of your friends ?
>
> {
>  "title": "Learning how to surf",
>  "author": "Carla"
> }
>
> You can get the latest posts from "Carla" easily. You can get the list
> of your friends easily, but you will have to manually loop through
> your friends records in order to get the "latest posts from friends"
> or maybe I am missing something important here...


You're not. But what's the big deal? What you're describing can be done with
two requests (the second being a multi-key get for latest post of each
friend). I doubt you'll run into many issues unless the friends list is
enormous, and I'm sure a better approach could be found.

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