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

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

it is showing error uncaught exception: error: { "$err" : "Unsupported projection option: comments.vCount", "code" : 13097 }
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>

Comments

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}}
                      ])

1 Comment

Error:Object [object Object] has no method 'aggregate' working fine in mongoshell but not in my program

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.