0

I've seen this question all over google/SO/mongo docs, and I've tried to implement the solution, but it's not working for me. I have the following test database:

> db.test.find().pretty()
{
    "_id" : ObjectId("56b4ab167db9acd913ce6e07"),
    "state" : "HelloWorld",
    "items" : [
        {
            "guid" : "123"
        },
        {
            "guid" : "124"
        },
        {
            "guid" : "123"
        }
    ]
}

And I want to sort by the "guid" element of items. Running the sort commands yields:

> db.test.find().sort( {"items.guid" : 1}).pretty()
{
    "_id" : ObjectId("56b4ab167db9acd913ce6e07"),
    "state" : "HelloWorld",
    "items" : [
        {
            "guid" : "123"
        },
        {
            "guid" : "124"
        },
        {
            "guid" : "123"
        }
    ]
}

How can I sort by the "guid" element, so that the returned output of "items" is the 123, 123, and 124 guids (essentially move the child elements of "items" so that they're sorted by "guid")?

EDIT: I've also tried to use the $orderby command, doesn't accomplish what I want:

> db.test.find({ $query : {}, $orderby: {'items.guid' : 1}  }).pretty()
{
    "_id" : ObjectId("56b4ab167db9acd913ce6e07"),
    "state" : "HelloWorld",
    "items" : [
        {
            "guid" : "123"
        },
        {
            "guid" : "124"
        },
        {
            "guid" : "123"
        }
    ]
}
David
  • 1,454
  • 3
  • 16
  • 27
  • If all your `guid` are of 3 digits then sorting is possible using `aggregate` command else it will be difficult because they are not numerical, they are string. – dikesh Feb 05 '16 at 14:09
  • have a look at this: https://docs.mongodb.org/v3.0/reference/operator/meta/orderby/ – Skywalker Feb 05 '16 at 14:10
  • @dikesh: could you please provide an example of the aggregate command that'll accomplish the sorting? – David Feb 05 '16 at 14:11
  • @LorenzovonMatterhorn: I tried the orderby, didn't work (see edit)... – David Feb 05 '16 at 14:13
  • 1
    have a look at this SO question: http://stackoverflow.com/questions/13449874/how-to-sort-array-inside-collection-record-in-mongodb – Skywalker Feb 05 '16 at 14:15
  • Thanks, the aggregate command worked. It's so not-intuitive, but maybe that's just because I'm new to mongo. – David Feb 05 '16 at 14:19

1 Answers1

0

Here is how it can be done using aggregate

db.test.aggregate([
    {
        $unwind : '$items'
    },
    {
        $sort : {'items.guid' : 1}
    },
    {
        $group : {
            _id : '$_id',
            state : {$first : '$state'},
            items : {
                $push : {'guid' : '$items.guid'}
            }
        }
    }
]).pretty()

This is the output from this command.

{
    "_id" : ObjectId("56b4ab167db9acd913ce6e07"),
    "state" : "HelloWorld",
    "items" : [
        {
            "guid" : "123"
        },
        {
            "guid" : "123"
        },
        {
            "guid" : "124"
        }
    ]
}
dikesh
  • 2,977
  • 2
  • 16
  • 26