2016-10-12 5 views
2

質問背景私は、golang mgo.v2 liabaryを使用してMongoDBデータから集約データを取得したいと考えています。golang mgo.v2 liabryでMongoDBを使用して集計Piplineの結果を得る方法

following.collection名前は私がimplemeningたときに迷惑していますcommunity.but私はすでに、スタックオーバーフローからの助けによって私が欲しいのMongoDBシェルスクリプトを解決したいるUserAgent

{ 
    "_id" : ObjectId("57f940c4932a00aba387b0b0"), 
    "tenantID" : 1, 
    "date" : "2016-10-09 00:23:56", 
    "venueList" : [ 
     { 
      "id" : “VID1212”, 
      "sum" : [ 
       { 
         "name" : "linux", 
         "value" : 12 
       }, 
       { 
        "name" : "ubuntu", 
        "value" : 4 
       } 
      ], 
      “ssidList” : [ // this is list of ssid’s in venue 
       { 
        "id" : “SSID1212”, 
        "sum" : [ 
         { 
          "name" : "linux", 
          "value" : 8 
         }, 
         { 
          "name" : "ubuntu", 
          "value" : 6 
         } 
        ], 
        “macList” : [ // this is mac list inside particular ssid ex: this is mac list inside the SSID1212 
         { 
          "id" : “12:12:12:12:12:12”, 
          "sum" : [ 
           { 
            "name" : "linux", 
            "value" : 12 
           }, 
           { 
            "name" : "ubuntu", 
            "value" : 1 
           } 
          ] 
         } 
        ] 
       } 
      ] 
     }, 
     { 
      "id" : “VID4343”, 
      "sum" : [ 
       { 
        "name" : "linux", 
        "value" : 2 
       } 
      ], 
      "ssidList" : [ 
       { 
        "id" : “SSID4343”, 
        "sum" : [ 
         { 
          "name" : "linux", 
          "value" : 2 
         } 
        ], 
        "macList" : [ 
         { 
          "id" : “43:43:43:43:43:34”, 
          "sum" : [ 
           { 
            "name" : "linux", 
            "value" : 2 
           } 
          ] 
         } 
        ] 
       } 
      ] 
     } 
    ] 
} 

であるように私は、収集データセットを持っていますそれはmgo.v2ライブラリを使用してゴランで。

これは私がpiplineを作成し、pipe.Allにそれを提供しているquestion background

かかわら行くと

func GetBrowserStats(constrains models.Constrains) ([]bson.M, error) { 

    session := commons.GetMongoSession() 
    defer session.Close() 
    var col = session.DB("analytics").C("useragents") 

    pipeline1 := bson.M{ 
     "$match": bson.M{ 
      "venueList.id": bson.M{ 
       "$in": []string{"VID1212", "VID4343"}, 
      }, 
     }, 
    } 
    pipeline2 := bson.M{ 
     "$unwind": "$venueList", 
    } 
    pipeline3 := bson.M{ 
     "$match": bson.M{ 
      "venueList.id": bson.M{ 
       "$in": []string{"VID1212", "VID4343"}, 
      }, 
     }, 
    } 
    pipeline4 := bson.M{ 
     "$unwind": "$venueList.sum", 
    } 

    pipeline5 := bson.M{ 
     "$group": bson.M{ 
      "_id": "$venueList.sum.name", 
      "count": bson.M{ 
       "$sum": "$venueList.sum.value", 
      }, 
     }, 
    } 
    pipeline6 := bson.M{ 
     "$group": bson.M{ 
      "_id": bson.NewObjectId(), 
      "counts": bson.M{ 
       "$push": bson.M{ 
        "name": "$_id", 
        "value": "$count", 
       }, 
      }, 
     }, 
    } 

    all := []bson.M{pipeline1, pipeline2, pipeline3, pipeline4, pipeline5, pipeline6} 
    pipe := col.Pipe(all) 

    result := []bson.M{} 
    err := pipe.All(&result) 
    println(result[0]) 
    if err != nil { 
     println(err.Error()) 
     errMsg := "Error occourred while getting dashboard configs from mongo stack:" + err.Error() 
     log.Error() 
     return result, errors.New(errMsg) 
    } 
    return result, nil 
} 

を以下のように私はgolangコードを実装してくださいMongoDBのシェルスクリプト

db.useragents.aggregate([ 
    { "$match": { "venueList.id": { "$in": ["VID1212", "VID4343"] } } }, 
    { "$unwind": "$venueList" }, 
    { "$match": { "venueList.id": { "$in": ["VID1212", "VID4343"] } } }, 
    { "$unwind": "$venueList.sum" },  
    { 
     "$group": { 
      "_id": "$venueList.sum.name", 
      "count": { "$sum": "$venueList.sum.value" } 
     } 
    }, 
    { 
     "$group": { 
      "_id": null, 
      "counts": { 
       "$push": { 
        "name": "$_id", 
        "value": "$count" 
       } 
      } 
     } 
    } 
]) 

です()が返されますが、resultから返されるnullの結果が返されます。

私は最後に、私はsolution.Iはstackoverflowのコミュニティとそれを共有したい見つけるresult

{ "_id" : ObjectId("57f73573d6e0ac1a9f2ab346") , "counts" : [ { "name" : "ubuntu", "value" : 1 }, { "name" : "linux", "value" : 14 } ] } 

答えて

1

で次のオブジェクトを返すようにしたいです。

私はこの1つ参照してください。google group answer

session := commons.GetMongoSession() 
    defer session.Close() 

    pipeline := []bson.D{ 
     bson.D{ 
      {"$match", 
       bson.M{ 
        "venueList.id": bson.M{"$in": []string{"VID1212", "VID4343"}}, 
       }, 
      }, 
     }, 
     bson.D{ 
      {"$unwind", "$venueList"}, 
     }, 
     bson.D{ 
      {"$match", 
       bson.M{ 
        "venueList.id": bson.M{"$in": []string{"VID1212", "VID4343"}}, 
       }, 
      }, 
     }, 
     bson.D{ 
      {"$unwind", "$venueList.sum"}, 
     }, 
     bson.D{ 
      {"$group", 

       bson.M{ 
        "_id": "$venueList.sum.name", 
        "count": bson.M{ 
         "$sum": "$venueList.sum.value", 
        }, 
       }, 
      }, 
     }, 
     bson.D{ 
      {"$group", 
       bson.M{ 
        "_id": bson.NewObjectId(), 
        "counts": bson.M{ 
         "$push": bson.M{ 
          "name": "$_id", 
          "value": "$count", 
         }, 
        }, 
       }, 
      }, 
     }, 
    } 

    query := bson.D{ 
     {"aggregate", "useragents"}, // useragents is a collection name 
     {"pipeline", pipeline}, 
    } 

    var res interface{} 
    err := session.DB("analytics").Run(query, &res) 
    if err != nil { 
     println(err.Error()) 
    } else { 
     println(res) 
    } 
関連する問題