mongodb文档分组

时间:2012-04-19 06:23:31

标签: mongodb

我的测试集合如下所示..

> db.test.find()
{ "_id" : "111-21212121", "Mawb" : "111-1111111", "Hawb" : "111111", "Hawb_date" : "date1" }
{ "_id" : "111-21212122", "Mawb" : "111-1111111", "Hawb" : "222222", "Hawb_date" : "date1" }
{ "_id" : "111-21212123", "Mawb" : "222-2222222", "Hawb" : "333333", "Hawb_date" : "date1" }
{ "_id" : "111-21212124", "Mawb" : "222-2222222", "Hawb" : "444444", "Hawb_date" : "date1" }
{ "_id" : "111-21212125", "Mawb" : "333-3333333", "Hawb" : "555555", "Hawb_date" : "date1" }
{ "_id" : "111-21212126", "Mawb" : "333-3333333", "Hawb" : "666666", "Hawb_date" : "date1" }

我希望用“Mawb”键对它进行分组(映射/组合)... 所以我可以输出分组文档,如

[
{ "_id" : "111-21212121", "Mawb" : "111-1111111", "Hawb" : "111111", "Hawb_date" : "date1" }
{ "_id" : "111-21212122", "Mawb" : "111-1111111", "Hawb" : "222222", "Hawb_date" : "date1" }],
[{ "_id" : "111-21212123", "Mawb" : "222-2222222", "Hawb" : "333333", "Hawb_date" : "date1" }
{ "_id" : "111-21212124", "Mawb" : "222-2222222", "Hawb" : "444444", "Hawb_date" : "date1" }],
[{ "_id" : "111-21212125", "Mawb" : "333-3333333", "Hawb" : "555555", "Hawb_date" : "date1" }
{ "_id" : "111-21212126", "Mawb" : "333-3333333", "Hawb" : "666666", "Hawb_date" : "date1" }]

我已经尝试过降低地图,但无法通过。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

如果您使用的是MongoDB 2.1或更高版本(2.1.x是预计很快会发布到2.2.0稳定版本的开发系列),您可以使用聚合框架在服务器端执行所有这些操作。

http://www.mongodb.org/display/DOCS/Aggregation+Framework