views:

237

answers:

2

I'm interested in document-oriented databases, and I'd like to play with MongoDB. So I started a fairly simple project (an issue tracker), but am having hard times thinking in a non-relational way.

My problems:

  1. I have two objects that relate to each other (e.g. issue = {code:"asdf-11", title:"asdf", reporter={username:"qwer", role:"manager"}} - here I have a user related to the issue). Should I create another document 'user' and reference it in 'issue' document by its id (like in relational databases), or should I leave all the user's data in the subdocument?

  2. If I have objects (subdocuments) in a document, can I update them all in a single query?

A: 

The beauty of mongodb and other "NoSQL" product is that there isn't any schema to design. I use MongoDB and I love it, not having to write SQL queries and awful JOIN queries! So to answer your two questions.

1 - If you create multiple documents, you'll need make two calls to the DB. Not saying it's a bad thing but if you can throw everything into one document, why not? I recall when I used to use MySQL, I would create a "blog" table and a "comments" table. Now, I append the comments to the record in the same collection (aka table) and keep building on it.

2 - Yes ...

luckytaxi
Sure, it makes sense to keep comments in the blog object, but normalisation/DRY still applies: it's a bad form to put the same data in multiple places so if you want to have users appear more than once throughout a collection, it's probably better to reference them by ID and put them in their own collection. The beauty is that you don't have to create special collections just to express many-to-many relationships.
Alan
A: 

I develop a BugTracker in MongoDB too, ouspnow

I have User in twice document.

I have some complete Document with User and in all Issue I have some link to my User and all information really need in this Issue, like email, username etc..

Don't hesitate to duplicate your data in several Document. You just need update it all

shingara