morphia聚合不同的地图键

时间:2015-09-29 16:08:55

标签: java mongodb aggregation-framework morphia

我有一个mongo集合,其中一列是map。例如:

   "_id" : ObjectId("5621b"),
    "creationDate" : ISODate("2015-09-29T14:27:37.451Z"),
    "comment" : "this is my votes",
    "attributes" : {
        "java" : "great",
        "perl" : "good",
        "ruby" : "so so",
         }
}

{
    "_id" : ObjectId("5621c"),
    "creationDate" : ISODate("2015-09-29T15:03:25.948Z"),
    "comment" : "foo",
    "attributes" : {
        "perl" : "old fashioned",
        "java" : "best of all",
        "bash" : "very usefull",
        "lisp" : "plan to learn",
         }

我正在使用java和morphia,想要获得某种聚合

 datastore.createAggregation(LanguageComments.class)
      .group(

我可以在哪里获取属性map的所有键: java,perl,ruby,bash,lisp

1 个答案:

答案 0 :(得分:0)

db.yourcollectionname.distinct(' attributes')将为上面显示的示例数据生成以下结果

[         {                 "的java" :"伟大",                 " perl的" :"好",                 "红宝石" :"如此"         },         {                 " perl的" :"老式",                 "的java" :"最重要的",                 "的bash" :"非常有用",                 "口齿不清" :"计划学习"         } ]

或者您可以使用聚合查询

db.yourcollectionname.aggregate([{$组:{_ ID:' $属性'}}])

它会给你以下结果

{" _id" :{" perl" :"老式"," java" :"最重要的"," bash" :"非常有用"," lisp" :"计划学习" }}

{" _id" :{" java" :"伟大"," perl" :"好","红宝石" :"如此" }}

选择最适合您的方案的查询,执行该查询并将输出放在Java Map中。