0

Being new to MongoDB I'm not really sure what would be the best practice for this.

Currently Posts have a set of tags assigned to them, and I'd like to include at the bottom of each post a "Related Posts" section. What would be the different/best way(s) to accomplish this in MongoDB?

My initial guess is that I would scan for which posts have ANY of the parent post's tags, then I'd cycle through the posts and count how many of the parent post's tags they have. The ones that have the highest amount of same tags would be first. Though this seems incredibly ineffecient.

balupton
  • 47,113
  • 32
  • 131
  • 182
  • @sentinel is right if you show us your data model we'll be better able o help you – Juan Gomez May 07 '11 at 15:21
  • Give us a couple of sample documents and you'll likely get a better reply. – Gates VP May 08 '11 at 05:38
  • There's also a question about this type of M:N mapping here: http://stackoverflow.com/questions/4662530/how-should-i-implement-this-schema-in-mongodb/4684647#4684647 – Gates VP May 08 '11 at 05:38

1 Answers1

1

Thanks guys, I ended up storing the related documents inside the parent document.

To find related documents, I found all which have ANY of the parent document tags, then sort by how many tags they mutually have.

Here is the logic: https://github.com/bevry/docpad/blob/2bcfd152b4b55bab8944c968b8bf093300925319/lib/docpad.coffee#L297

Here is the schema: https://github.com/bevry/docpad/blob/2bcfd152b4b55bab8944c968b8bf093300925319/lib/docpad.coffee#L85

balupton
  • 47,113
  • 32
  • 131
  • 182