views:

54

answers:

1

Hi, I'm starting to learn MongoDB, using the NoRM C# driver in an ASP.NET MVC project. I'm just writing POCO classes right now, and have question on how to implement relationships between Blog Posts, Comments, and Tags. I think I have the Post & Comment, but not sure what to do on the Tags. In SQL, they are many-to-many relationship, how would I implements something similar with MongoDB & NoRM?

These are my classes for Post & Comment:

public class Post
    {
        public ObjectId _id { get; set; }
        public string Title { get; set; }
        public string Post { get; set; }
        public string Uri { get; set; }
        public DateTime Date { get; set; }
    }

public class Comment
    {
        public ObjectId _id { get; set; }
        public DbReference<Post> Post { get; set; }
        public string Comments { get; set; }
        public string Author { get; set; }
        public string Email { get; set; }
        public string Url { get; set; }
        public DateTime Date { get; set; }
    }

My Tag object is the one in question, how can I relate tags <==> posts.

public class Tag
    {
        public ObjectId _id { get; set; }
        public string Name { get; set; }
    }

Thanks.

+3  A: 

You don't.

You're working with relationship-less NoSql now which requires a completely different mindset. Tags become part of the the Post and do not exist on their own.

I can't answer any better than linking you to this blog post: That No SQL Thing: The relational modeling anti pattern in document databases

jfar
Indeed, you embed the tags, the nice thing is that you can still index those tags.
TTT
Then how would I implement such a blog that contains multiple tags, and click on a tag would bring blogs that has the same tag? Similar to Stackoverflow? Thanks.
Saxman
@Saxman, you'd have a tags attribute as a string on your BlogPost, then do a contains search on that field. NoRM supports that out of the box.
jfar
Thanks all. I need to change my mind set on implementing document database :)
Saxman