views:

40

answers:

2

Is there a way to sort sub docs in a mongo query? Example(Blog Collection):

{
    "_id" : ObjectId("4c69d19532f73ad544000001"),
    "content" : "blah blah blah",
        "comments" : {
{"author": "jim",   "content":"comment content 1", "date" : "07-24-1995"},
{"author": "joe",   "content":"comment content 2",  "date" : "07-24-1996"}
{"author": "amy", "content":"comment content 3", "date" : "09-10-1999"}
        }
}
{
    "_id" : ObjectId("4c69d19532f73ad544000002"),
    "content" : "blah blah blah",
        "comments" : {
{"author": "jim",   "content":"comment content 1",  "date" : "07-24-1995"},
{"author": "joe",   "content":"comment content 2",  "date" : "07-24-1996"}
{"author": "amy", "content":"comment content 3",  "date" : "07-24-1997"}
        }
}

I want to get my blog posts ordered in however way I deiced and then have comments ordered within my blog posts by reverse order by date or any other sorting I want.. Is that not possible with mongoDB?

A: 

I dont think you can. I raised same question on the forums.

luckytaxi
+1  A: 

It's not possible directly out of MongoDB, however when you pull the document you can then sort the array as if it was an array of objects, using whatever native sort method your language has. This is how I do comments on my blog with MongoDB.

PHP Code

/* Takes an array of comments and turns them into native objects */
public static function loadMany($pcomments)
{
    $comments = array();
    if(isset($pcomments) && count($pcomments) > 0)
    {
        foreach($pcomments as $key => $comment)
        {
            $comment['data']['index'] = $key;
            $comments[] = Comment::loadOne($comment['data']);
        }
    }
    usort($comments, "comment_compare");
    return $comments;
}

/* Compares comment timestamps */
function comment_compare($a, $b)
{
    if($a->timestamp->sec == $b->timestamp->sec)
    {
        return 0;
    }
    return ($a->timestamp->sec < $b->timestamp->sec) ? -1 : 1;
}
Josh K