0

This is my document i want to sort array documents by ascending order to get so for that my queries are in following code.but i am not getting the docs in sorted way.

The query is

db.sample.find({_id: ObjectId("55b32f5957e47fabd30c5d2e")}).sort({'naresh.ts':1}).pretty();

This is the result I am getting

{
    "_id" : ObjectId("55b32f5957e47fabd30c5d2e"),
    "naresh" : [
        {
            "ts" : "hi",
            "created_by" : 1437806425105
        },
        {
            "ts" : "hello",
            "created_by" : 1437806425105
        },
        {
            "ts" : "waht",
            "created_by" : 1437807757261
        },
        {
            "ts" : "lefo",
            "created_by" : 1437807768514
        },
        {
            "ts" : "lefow",
            "created_by" : 1437807775719
        }
    ]
}
ZeMoon
  • 20,054
  • 5
  • 57
  • 98
Naresh Kumar
  • 276
  • 2
  • 10
  • 1
    Duplicate of http://stackoverflow.com/questions/13449874/how-to-sort-array-inside-collection-record-in-mongodb – Adam Jul 25 '15 at 07:45
  • You need to use aggregation if you want to sort an array inside the document. – ZeMoon Jul 25 '15 at 07:46
  • so without aggregation can't i sort the documents.with aggregation its working but in stack overflow some of the people solved directly with out aggrregation but its not working for me – Naresh Kumar Jul 25 '15 at 07:49

3 Answers3

0

You can use $aggregation like following query:

db.collection.aggregate({
    "$match": {
    "_id": ObjectId("55b32f5957e47fabd30c5d2e")
    }
}, {
    $unwind: "$naresh"
}, {
    $sort: {
    "naresh.ts": 1
    }
}, {
    "$group": {
    _id: "$_id",
    "naresh": {
        $push: "$naresh"
    }
    }
})
Vishwas
  • 6,967
  • 5
  • 42
  • 69
0

The cursor .sort() only looks at the values in the array to decide to use the "smallest" value of the specified field ( in ascending order ) to determine how to "sort" the documents in the response. This does not "sort" the array content itself.

In order to sort the array, you need to use the aggregation framework to manipulate the document:

db.sample.aggregate([
    { "$match": { "_id": ObjectId("55b32f5957e47fabd30c5d2e") },
    { "$unwind": "$naresh" },
    { "$sort": { "$naresh.ts": 1 } },
    { "$group": {
        "_id": "$_id",
        "naresh": { "$push": "$naresh" }
    }}
])

That sorts the array.

Better yet, if you "always" want then results sorted then do it as you update the document:

db.sample.update({},{ "$push": { "$each": [], "$sort": { "ts": 1 } } },{ "multi": true })

And use those same, $each and $sort modifiers when adding new elements to the array and the content will remain sorted.

Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
0

If you want just query the collection and get the output sorted, then Blackes Seven's answer will work perfectly for you.

However if you want to update the documents in the sorted order, go with this update query:

update(
    {
        _id: ObjectId("55b32f5957e47fabd30c5d2e")
    }, 
    {
        $push: {
            naresh: {
                $each: [], 
                $sort: {created_by: 1}
            }
        }
    }
)
Community
  • 1
  • 1
bagrat
  • 7,158
  • 6
  • 29
  • 47