3

I am currently building a charity student support website, to help African students that want to apply to American Universities. I want to build a threaded comments system. I was planing on using MongoDB, (backend is all java). I'm fairly new to MongoDB and therefore it is taking me a while to absorb all the information. I have some questions for you:

Does anyone know of any implementation out there that I can take a look at? I wanted to have this done by the end of the weekend.

I was thinking of doing something like this:

conversation
{ 
  _id: BigInt, 
  entityID: BigInt, //reference to what is being commented
  comments:[
    status: String (approved, spam, removed),  
    UID: BigInt,  
    timestamp: date  
    commentText: String
    likeCount: int
    replies:[
      status: String (approved, spam, removed),  
      UID: BigInt,  
      timestamp: date  
      commentText: String
      likeCount: int
    ]
  ]
}

Thanks a lot for the help,

nos
  • 223,662
  • 58
  • 417
  • 506
Anthony Silva
  • 195
  • 1
  • 14
  • This is pretty much the same question and explained in dozens of slides and talks and documentation. The standard "blog post" example is used everywhere. http://www.mongodb.org/display/DOCS/Schema+Design –  Jul 08 '11 at 03:37
  • The problem with most implementations I see is that if I embed the replies into the comments, It becomes hard to update the replies. Let's say I want to change the status of a reply from approved to removed. How could I do that with embedded objects? – Anthony Silva Jul 08 '11 at 06:01
  • Can anyone help? I have searched around and I still cannot find a solution. Thanks – Anthony Silva Jul 12 '11 at 20:32
  • @Anthony: if you store comments in an array you can't. The best option you have is to add a 'deleted' field to it and don't display it. – Karoly Horvath Jul 17 '11 at 19:37
  • BTW BigInt as _id? how do you make it unique? – Karoly Horvath Jul 17 '11 at 19:39

1 Answers1

0

You could use a nested set model using one flat collection for the comments. It's a bit of work to maintain (specially because you would have a multiroot, multitree collection) but it would be more scalable and easier to manipulate and query.

You would have:

conversations {
  _id: ObjectId,
  whatever fields you need,
}
comments {
  _id: ObjectId,
  parentId: ObjectId,
  conversationId: ObjectId,
  authorId: ObjectID,
  commentText: String,
  lft: Int,
  rght: Int,
  ...
}

I'm not familiar with the Java driver but doesn't it have a specific type for _id fields? This would give you free access to the timestamp and give you a unique id for everything.

hlidotbe
  • 888
  • 2
  • 9
  • 17