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.
- What’s the best way of doing this?
- 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.
Couldn’t you use the Slice command for retrieving a subset of the array (the last 10)?
Something like:
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: