Reputation: 24583
Data:
{
"properties" : {
"user" : ObjectId("51d3053627f4169a52000005"),
"createdOn" : ISODate("2013-07-02T18:00:03.841Z")
},
"_id" : ObjectId("51d31a87716fb81a58000003"),
"geometry" : {
"type" : "Point",
"coordinates" : [ 10, 10 ]
}
},{
"properties" : {
"user" : ObjectId("51d3053627f4169a52000005"),
"createdOn" : ISODate("2013-07-02T18:23:03.841Z")
},
"_id" : ObjectId("51d31a87716fb81a58000003"),
"geometry" : {
"type" : "Point",
"coordinates" : [ 20, 20 ]
}
}
And I am trying the following query:
db.locations.aggregate(
{ $group: {
_id: "$properties.user",
locations: {
$push: {
location: {geometry: "$geometry", properties: "$properties"}
}
}
}},
{ $sort: { "properties.createdOn": 1 }}
);
No matter which direction I change the sort flag (1/-1) the order of my results does not change.
Any ideas?
Upvotes: 0
Views: 133
Reputation: 312149
After the $group
, your pipeline docs only contain those fields defined in the $group
, so properties.createdOn
doesn't exist for the $sort
operation.
Move your $sort
before the $group
in the pipeline instead:
db.locations.aggregate(
{ $sort: { "properties.createdOn": 1 }},
{ $group: {
_id: "$properties.user",
locations: {
$push: {
location: {geometry: "$geometry", properties: "$properties"}
}
}
}}
);
Upvotes: 3