incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Benjamin Reed <rangerr...@gmail.com>
Subject Re: Design Question: What is a Good Model?
Date Thu, 10 Oct 2013 12:47:29 GMT
Thanks for all the suggestions everyone.  Between you guys and #couchdb I
got things working nicely.

Given a fixture like this (please excuse my fanciful test data <g>):

[{
'_id': '1',
'type': 'event',
'username': 'official',
'summary': 'Murder',
'description': 'You will be murdered.',
'isPublic': true
},
{
'_id': '2',
'type': 'event',
'username': 'ranger',
'summary': 'Dying',
'description': 'I will be dying.',
'isPublic': true
},
{
'_id': '3',
'type': 'event',
'username': 'bob',
'summary': 'Living',
'description': 'I am totally going to continue living.',
'isPublic': true
},
{
'_id': '4',
'type': 'event',
'username': 'ranger',
'summary': 'Private',
'description': "It's a priiiivate event, dancin' for money, do what you
want it to do.",
'isPublic': false
},
{
'type': 'favorite',
'username': 'ranger',
'eventId': '3'
}]

...I'm able to get the set of documents I made + favorited with just a map
of:

map: function(doc) {
if (doc.type === 'event') {
emit(doc.username, {'_id': doc._id, 'type': doc.type});
} else if (doc.type === 'favorite') {
emit(doc.username, {'_id': doc.eventId, 'type': doc.type});
}
}

and include_docs true, using username as the key.

I think map/reduce is finally clicking.  :)



On Thu, Oct 10, 2013 at 6:34 AM, Mark Deibert <mark.deibert@gmail.com>wrote:

> @Florian: It's not that simple. Pirate Ben's question was...
>
> "But what is the best way to retrieve the "favorites" associated with
> a specific
> user?".
>
> This will require 2 tables, perhaps 3. 2 if you go with the flatter
> approach of putting the favorites in an array attrib in the user. 3 if you
> want a favorites linking doc with user-2-event. I think it will be 2
> queries to get "user's favorite events" info regardless of whether you do 2
> or 3 docs. I think I'd opt for the 2 table version.
>
>
> On Wed, Oct 9, 2013 at 8:03 PM, Filippo Fadda <
> filippo.fadda@programmazione.it> wrote:
>
> > If you just want return the user favorites of course, you can just emit
> > userId as key and eventId as value, but if you want show them ordered by
> > timestamp (events belong in time), you must include the timestamp in the
> > key: emit([userId, timestamp], eventId). That because in CouchDB the
> query
> > results are _always_ ordered key.
> >
> > -Filippo
> >
> > On Oct 9, 2013, at 11:23 PM, Florian Westreicher Bakk.techn. wrote:
> > > Quick question: why would a complex key be required? Could we not emit
> > (userid, eventid) and be happy?
> >
>



-- 
Benjamin Reed a.k.a. Ranger Rick
Fink, KDE, and Mac OS X development

Blog: http://www.raccoonfink.com/
Music: http://music.raccoonfink.com/

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