3

I have a comment collection like this

{
  _id: 'c1',
  text: 'comment 1',
  votes: 1,
  replies: [
    {
      _id: 'r1',
      text: 'reply 1',
      isReply: true,
      votes: 3
    },
    {
      _id: 'r2',
      text: 'reply 2',
      isReply: true,
      votes: 0
    }
  ]
},
{
  _id: 'c2',
  text: 'comment2',
  votes: 2,
  replies: []
}

The idea is a comment can have many replies. All comments and replies have id, text, votes. How can I get the best 2 comment or reply having the most votes. In the given case, this will be 'reply 1' and 'comment 2'.

I can send 1 request to mongodb to have the best 2 comments and another one to have the best 2 replies, then compare them to get what I want.

But, can it be possible with only 1 request to mongodb, I can have the result like this?

 [
   {
     _id: 'r1',
     text: 'reply 1',
     isReply: true,
     votes: 3
   },
   {
     _id: 'c2',
     text: 'comment 2',
     votes: 2
   }
 ]

Or better can I flatten the comment/replies to have a list of comment or reply sorted by votes? In this case it will be [r1, c2, c1, r2] with respectively their properties

Thanks.


Update: I tried aggregate({$unwind: '$replies'}) but I still have comments and replies on 2 different levels that I cannot compare by using the aggregation framework. Maybe be there is a way to flatten these 2 levels, I am very new to mongodb.

3
  • Could you please edit your question to include the details of what aggregation pipelines you've tried? I'd imagine you might need to do something like this: docs.mongodb.org/manual/tutorial/… Sep 16, 2013 at 15:44
  • do you need all the properties or can just its id and number of votes be enough? i.e. [{id:'r1',votes:X}, etc]? Sep 19, 2013 at 20:32
  • @AsyaKamsky I also need other properties as stated in the example of result.
    – lastid
    Sep 19, 2013 at 21:50

2 Answers 2

4

A little change of the schema to denormalize the votes would make it easier to sort. If there is another array of votes embedded in the comment document, like:

votes: [ 
  { "type" : "c", "_id": "c1", v: 1},
  { "type" : "r", "_id": "r1", v: 3},
  { "type" : "r", "_id": "r2", v: 2}
]

querying and sorting could be straight-forward.

db.playground.aggregate(
[
  {$project: { votes: 1 }},
  {$unwind: "$votes"},
  {$sort: {"votes.v": -1}},
  {$limit: 2}
])

It gives the following result.

{
  "result" : [
    {
      "_id" : "c1",
      "votes" : {
        "type" : "c",
        "_id" : "c1",
        "v" : 3
      }
    },
    {
      "_id" : "c1",
      "votes" : {
        "type" : "r",
        "_id" : "r1",
        "v" : 2
      }
    }
  ],
  "ok" : 1
}

An index on votes.v is needed because it seems like a read heavy use case. When updating the comments, just update votes array in the same update request.

6
  • If I understand correctly, this is what you would do, because I cannot format code in comment I created a github gist here . It does not seem straight forward to me to request the 2 best comment or reply. Can you give me an example please.
    – lastid
    Sep 19, 2013 at 22:39
  • @lastid, you gist looks good to me. I improved my answer to give an example of aggregation query.
    – visualzhou
    Sep 20, 2013 at 18:13
  • As stated in my question and in my gist, I need 2 best comment or reply with their content (text). With your solution, you get the best 2 ids, then from these 2 ids get their content right? So you will have at least 2 requests, maybe 3. Your data structure is tricky too, if you need only 1 reply and its vote, you need to get the reply from the array replies and also the votes property, then do a loop to know which votes value is for the reply in question. A solution with 2 requests I already stated in my question, and I preferred that way because votes is bond to each reply or comment.
    – lastid
    Sep 20, 2013 at 22:53
  • Regarding to fetching their contents, yes, you are right. Actually, your solution makes a lot of sense but it involves client side merge. Now we are talking about database side solution. My first thought is to have votes in comments and replies and duplicate them in another array. So querying a reply is as usual. The root problem is you'd like to treat comments and replies as in hierarchy when retrieving them, and as the same kind of thing when voting them. So the data model has to be something mixed. Right?
    – visualzhou
    Sep 20, 2013 at 23:23
  • If votes is what you care about the most, make reply as top-level document as comments, and store replies' _ids in an array in comment. Thus getting comments requires two requests. By the way, I don't think there is an easy way to get only one item in an array in a document with your current design, since document is the basic unit for processing.
    – visualzhou
    Sep 20, 2013 at 23:35
0

I suggest you to not put replies in comment object. DbObject has 16mb maximum size. If a comment got many replies, this structure would fail. Instead of this structure, you may keep replies in comment collection and put a parent comment id into reply object(you may want to put a field like {type: 'reply'} or {type: 'comment'} But parentCommentId existence will provide which one is in what type). In this way, you can easily query on both comments and replies.

I also want to add that aggregation queries are not good for UI response. I don't know your use case but don't forget it.

3
  • If I do that it will cost me 2 queries to display the first 10 comments and their replies. And then because I am taking advantage of reactivity in Meteor, it's easier for me to embed replies in each comment I didnt know aggregation queries are slow...
    – lastid
    Sep 19, 2013 at 22:40
  • aggregation queries may or may not be slow. it depends on whether there are indexes to support them, and how much work they are doing. Sep 20, 2013 at 5:10
  • It's slow for UI actions, espacially grouping. Sep 20, 2013 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.