1

Let's imagine a usual blog engine (just for example). The model would consist of Posts collection with embedded Comments "collection".

Now, I need to get only 10 recent comments along with my Post data.

  1. What's the best way of doing this?
  2. Is this a valuable optimization? (apart from reducing network traffic)

P.S. I use official C# driver + fluent-mongo but I can give up linq for a good cause.

Kostassoid
  • 1,870
  • 2
  • 20
  • 29
  • not an answer directly, but you may take a look at CQRS pattern... the idea, in your case, should be to denormalize a view of the recents comments. [Ncqrs framework](http://ncqrs.org/) may be a good starting point – Steve B Aug 31 '11 at 12:12
  • @Steve: that link does not work for me :( – mnemosyn Aug 31 '11 at 12:26
  • thanks, Steve. i'm aware of CQRS. in fact, I use it in this project, but without separate "denormalized" storage, I use mongodb for everything. my question is related to query part obviously, i need a fast way to query only the necessary data for views. i take it your advice is to add another subset along with complete collection or separate collection with just view data? this i can do but perhaps there's other way. – Kostassoid Aug 31 '11 at 12:26
  • @Kostassoid, take a look here this site might help http://www.chrisedwards.dreamhosters.com/blog/2010/05/29/advanced-mongodb-queries-in-c-using-the-norm-driver-part-1/ – Jethro Aug 31 '11 at 13:42
  • In your title you say "subcollection," I think you mean embedded array instead. Subcollections are a different concept and your title is misleading. – Zaid Masud Oct 18 '11 at 17:09
  • @zooone9243, I meant what I wrote. As far as I know there's no other "subcollection" concept in mongodb context. "Embedded collection" (not array) or "Virtual collection" (hopefully soon) would be another synonyms. – Kostassoid Oct 19 '11 at 12:34
  • @Kostassoid subcollections are also used to refer to collection "namespaces" using dot notation. See here: Collections can be organized in namespaces; these are named groups of collections defined using a dot notation (http://www.mongodb.org/display/DOCS/Collections). The book "MonogDB The Definitive Guide" refers to subollections in this context as well. – Zaid Masud Oct 24 '11 at 15:15
  • 1
    @zooone9243, you're right, as a matter of fact i've recently found this book and started to read it. the reason why i haven't heard this term before is because they've obviously decided to drop it. but i'll update the title, thanks. – Kostassoid Oct 25 '11 at 09:19

2 Answers2

2

Couldn't you use the Slice command for retrieving a subset of the array (the last 10)? Something like:

db.posts.find({}, {comments:{$slice: -10}})

I found this on the official documentation when I had to do something similar.

Link: http://www.mongodb.org/display/DOCS/Retrieving+a+Subset+of+Fields#RetrievingaSubsetofFields-RetrievingaSubrangeofArrayElements

The easiest way I could find to use the slice command with C# is:

var your_query; 
var slice = Fields.Slice("comments", -10); 
var cursor = collection.Find(your_query).SetFields(slice); 
foreach (var document in cursor) { 
    ...
} 
jeffsaracco
  • 1,259
  • 12
  • 21
  • interesting, thanks. not really what i was looking for but indeed a good solution for some cases. and now i know 10gen is working on virtual collections which is exactly what i need, AFAIK. – Kostassoid Sep 06 '11 at 19:32
0

Why not use a dedicated collection of most recent comments? You'll have to perform two inserts when a comment is posted, but fetching the most recent comments is simple. Typically, you would fetch these much more often than a new comment is posted anyway.

As Steve B pointed out, this is typically a 'view' in the sense that this collection might contain slightly different information than the comments within the post collection. For example, you might want to store the post id and post name in each comment so you can display a corresponding link.

You could use a capped collection of, say 100 elements, which automatically drops old comments (i.e., implements a FIFO)

mnemosyn
  • 45,391
  • 6
  • 76
  • 82
  • yes, i know about capped collections, would be a good solution if i needed the latest comments for entire blog, but i need the latest for each Post. and "Posts" count in my case is > 1000000 and counting, i wouldn't know how to calculate a size for capped collection. using a dedicated collection looks like a good solution, i was just curious if there's a way to query data with dynamically set filters. – Kostassoid Aug 31 '11 at 12:40