2013-07-02 15 views
0

數據:MongoDB的aggreate,不能讓那種對日期字段工作

{ 
    "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 ] 
    } 
} 

,我試圖下面的查詢:

db.locations.aggregate(
    { $group: { 
    _id: "$properties.user", 
    locations: { 
     $push: { 
     location: {geometry: "$geometry", properties: "$properties"} 
     } 
    } 
    }}, 
    { $sort: { "properties.createdOn": 1 }} 
); 

無論哪個方向我更改排序標誌(1/-1)我的結果順序不變。

任何想法?

回答

3

$group之後,您的流水線文檔只包含$group中定義的那些字段,因此$sort操作不存在properties.createdOn

移動你的$sort在管道代替$group前:

db.locations.aggregate(
    { $sort: { "properties.createdOn": 1 }}, 
    { $group: { 
    _id: "$properties.user", 
    locations: { 
     $push: { 
     location: {geometry: "$geometry", properties: "$properties"} 
     } 
    } 
    }} 
); 
+0

完美,也有同樣的問題瓦特/限制爲好。做排序,限制,小組完美的作品。再次感謝@JohnnyHK – lostintranslation