0

数据:

{ 
  "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) 的哪个方向,结果的顺序都不会改变。

有任何想法吗?

4

1 回答 1

3

在 之后$group,您的管道文档仅包含在 中定义的那些字段$group,因此properties.createdOn该操作不存在$sort

将您的$sort之前$group移到管道中:

db.locations.aggregate(
  { $sort: { "properties.createdOn": 1 }},
  { $group: { 
    _id: "$properties.user", 
    locations: { 
      $push: {
        location: {geometry: "$geometry", properties: "$properties"} 
      } 
    }
  }}
);
于 2013-07-02T20:05:52.220 回答