Question

I currently have a Channels collection which is stored as the following:

{ "_id" : ObjectId("4f5d1012d48147e840000000"), "title" : "Testing", "description" : "hello!", "created" : "2012-03-11 20:50:26", "user" : ObjectId("4f5d0408d48147207f000000") }

But I would like users to now be able to subscribe to this channel. I don't think this important enough to be considered its own collection so I would like to store it as the following:

{ 
    "_id" : ObjectId("4f5d1012d48147e840000000"), 
    "title" : "Testing", 
    "description" : "hello!",
    "subscriptions" : 
        { "user" : ObjectId("USERIDHERE"), "created" : DATETIME },
        { "user" : ObjectId("USERIDHERE"), "created" : DATETIME },
        { "user" : ObjectId("USERIDHERE"), "created" : DATETIME }
    "created" : "2012-03-11 20:50:26", 
    "user" : ObjectId("4f5d0408d48147207f000000") 
}

Though I cant seem to get my head around how to cleanly implement this within my Lithium object. Would my $story->vote property be an array which I push to on every update?

Was it helpful?

Solution

IMHO, how you structure that will depend on how you will use subscriptions.

The reason I say this is because of performance concerns down the line with embedded mongo objects vs linked collections. http://www.mongodb.org/display/DOCS/Schema+Design

Instead of putting the user id with the subscription, I would propose linking the channel id with the user (channel id as an embedded collection in the user that subscribed).

The usage here makes sense because when you pull the user's collection (for their control panel or dashboard or front page, etc...) you will have the list of channels to display.

Embedding the user ids in the channel would only make sense if you plan on displaying channels and showing all the users that subscribe to them, kinda like the chat system in stack overflow.

Putting them in a separate collection would make sense if you plan on doing aggregations or will have more then one collection accessing subscriptions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top