Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is my mongodb document

{
    "_id" : "a3s6HzG9swNB3bQ78",
    "comments" : [ 
        {
            "cmt_text" : "opp",
            "vCount" : 2,

        }, 
        {
            "cmt_text" : "o2",

            "vCount" : 5,

        }, 
        {
            "cmt_text" : "o3",

            "vCount" : 3,

        }
    ],

    "question" : "test ques 3"
}

i want to sort the result using the vCount field how to achieve this

i tried the following but seems to be not working

Coll.findOne( {_id:this._id},{sort:{ "comments.vCount" : 1 }});

Coll.findOne( {_id:this._id},{sort:{ "comments.$.vCount" : 1 }});

anyone have idea about this???

EDIT

we are returning only one document and i want to display that document comment array values according to the vCount. my code

{{#each all_comments.comments}}

    <br>{{cmt_text}}</p>
{{/each}}

i want to display like below

o2
o3
opp

EDIT

this is working fine in shell

db.testCol.aggregate([
                    { $unwind: "$comments" },
                    { $group: { _id: { id:"$_id", vcount:"$comments.vCount"} } },
                    { $sort: { "_id.vcount":1 }}
                  ]) 

why is it not working in my meteor app it says

error:object has no method aggregate
share|improve this question

3 Answers 3

up vote 1 down vote accepted

aggregate isn't currently available on the client. You can just do a findOne, extract the comments array, and return a sorted version to the template. For example:

Template.allComments.helpers({
  comments: function() {
    var coll = Coll.findOne(this._id);
    return _.sortBy(coll.comments, function(comment) {
      return -comment.vcount;
    });
  }
});
<template name="allComments">
  {{#each comments}}
    <br>{{cmt_text}}</p>
  {{/each}}
</template>
share|improve this answer
    
working fine thx mate –  Sasikanth Feb 20 '14 at 3:32

You should learn aggregation for dealing with subdocuments, arrays and also with subdocuments in arrays. For your sort question this could work.

db.testCol.aggregate([
                    { $unwind: "$comments" },
                    { $group: { _id: { id:"$_id", vcount:"$comments.vCount"} } },
                    { $sort: { "_id.vcount":1 }}
                  ]) 

EDIT: In according to your edit you could add $project operator like;

db.testCol.aggregate([
                        { $unwind: "$comments" },
                        { $group: { _id: { id:"$_id", vcount:"$comments.vCount", text:"$comments.cmt_text"} } },
                        { $sort: { "_id.vcount":1 }},
                        { $project: { text: "$_id.text", _id:0}}
                      ])
share|improve this answer
    
Error:Object [object Object] has no method 'aggregate' working fine in mongoshell but not in my program –  Sasikanth Feb 19 '14 at 18:28

This is correct:

Coll.findOne( { _id: this._id }, { sort: { 'comments.vCount' : 1 } } );

No $ in front of sort.

EDIT:

If you want to sort the nested array, look here.

share|improve this answer
    
it is showing error uncaught exception: error: { "$err" : "Unsupported projection option: comments.vCount", "code" : 13097 } –  Sasikanth Feb 19 '14 at 17:15
    
edited the question –  Sasikanth Feb 19 '14 at 17:22
    
see my edit.... –  heinob Feb 19 '14 at 17:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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