0

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
Sasikanth
  • 3,045
  • 1
  • 22
  • 45

3 Answers3

3

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.

Community
  • 1
  • 1
heinob
  • 19,127
  • 5
  • 41
  • 61
  • it is showing error `uncaught exception: error: { "$err" : "Unsupported projection option: comments.vCount", "code" : 13097 }` – Sasikanth Feb 19 '14 at 17:15
2

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>
David Weldon
  • 63,632
  • 11
  • 148
  • 146
1

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}}
                      ])
quartaela
  • 2,579
  • 16
  • 63
  • 99
  • Error:Object [object Object] has no method 'aggregate' working fine in mongoshell but not in my program – Sasikanth Feb 19 '14 at 18:28