MongoDB:根据标准对文档进行分组

时间:2014-02-12 17:29:38

标签: mongodb

我正在使用Mongo 2.4。我有这个:

{
    "_id" : ObjectId(),
    "place" : "52f06eb4a0a9210c11000032",
    "message" : "Hi"
}
{
    "_id" : ObjectId(),
    "place" : "52f06eb4a0a9210c11000032",
    "message" : "Hi, how are you?"
}
{
    "_id" : ObjectId(),
    "place" : "32f06eb4a0a9210c11000032",
    "message" : "?"
}

我需要任何方法按“地点”分组文件。有意义吗?

1 个答案:

答案 0 :(得分:1)

db.collection.aggregate(
  {
    $group: {
      _id: "$place",
      messages: { $push: "$message" }
    }
  }
)

从MongoDB文档派生的示例:http://docs.mongodb.org/manual/reference/operator/aggregation/push/#grp._S_push