使用mgov2进行Mongodb查询。

huangapple go评论102阅读模式
英文:

Mongodb query using mgov2

问题

我有一系列在不同渠道上进行的端点测试。集合中的一个示例文档如下:

{
    "_id" : ObjectId("59959b30c699811077751b12"),
    "teststatus" : "Fail",
    "channelname" : "HouseController",
    "timestamp" : ISODate("2017-08-17T13:15:53.170Z"),
    "testid" : "llpKIgIfiiquqkSApwNn"
}

我正在查询以类似以下方式展示结果:

[
  {
    "Fail": 20,
    "Success Count": 30,
    "Total": 50,
    "channel": "c3"
  }, ...

但是我得到的成功和失败率计数是错误的。我当前在golang中的查询如下:

o1:=  bson.M{
	"$project" :bson.M{
		"channel":"$channelname",
		"time":"$timestamp",
		"teststatus" : "$teststatus",
		"_id":1,
	},
}
o2:=  bson.M{
	"$group" :bson.M{
		"_id": "$channel",
		"Success": bson.M{
			"$sum":bson.M{ "$eq" :"teststatus","Pass"},
		},
		"Total": bson.M{
			"$sum": 1,
		},
	},
}
o3:=  bson.M{
	"$project" :bson.M{
		"channel": "$_id",
		"Success Count":"$Success",
		"Total" : "$Total",
		"_id":0,
		"Fail": bson.M{
			"$subtract": []interface{}{"$Total", "$Success"},
		},
	},
}

我在计算成功计数时出错了。我无法弄清楚如何正确做。我刚刚开始使用mgo和golang。

提前感谢。

英文:

I have a collection of endpoint point tests conducted on various channels. A sample document from the collection is:

{
    "_id" : ObjectId("59959b30c699811077751b12"),
    "teststatus" : "Fail",
    "channelname" : "HouseController",
    "timestamp" : ISODate("2017-08-17T13:15:53.170Z"),
    "testid" : "llpKIgIfiiquqkSApwNn"
}

I am querying this to project the result something like this:

[
  {
    "Fail": 20,
    "Success Count": 30,
    "Total": 50,
    "channel": "c3"
  }, ...

But I am getting the wrong count for success and fail rate. My current query in golang looks like:

o1:=  bson.M{
	"$project" :bson.M{
		"channel": "$channelname",
		"time":"$timestamp",
		"teststatus" : "$teststatus",
		"_id":1,
	},
}
o2:=  bson.M{
	"$group" :bson.M{
		"_id": "$channel",
		"Success": bson.M{
			"$sum":bson.M{ "$eq" :"teststatus","Pass"},
		},
		"Total": bson.M{
			"$sum": 1,
		},
	},
}
o3:=  bson.M{
	"$project" :bson.M{
		"channel": "$_id",
		"Success Count":"$Success",
		"Total" : "$Total",
		"_id":0,
		"Fail": bson.M{
			"$subtract": []interface{}{"$Total", "$Success"},
		},
	},
}

I am doing wrong in the counting of success count. I just cant figure to do it right. I have just started with mgo and golang.

Thanks in advance

答案1

得分: 1

你需要使用$cond来进行条件计数。例如,以下代码可以一次性计算所有测试、失败的测试和成功的测试数量:

o2 := bson.M{
    "$group": bson.M{
        "_id": "$channel",
        "Total": bson.M{
            "$sum": 1,
        },
        "Success": bson.M{"$sum": bson.M{
            "$cond": []interface{}{
                bson.M{"$eq": []interface{}{"$teststatus", "Pass"}},
                1, 0,
            },
        }},
        "Fail": bson.M{"$sum": bson.M{
            "$cond": []interface{}{
                bson.M{"$eq": []interface{}{"$teststatus", "Fail"}},
                1, 0,
            },
        }},
    },
}
英文:

You need to use $cond to do conditional counting. For example the following counts all tests, failed ones and successful ones in one step:

o2 := bson.M{
    "$group" :bson.M{
        "_id": "$channel",
        "Total": bson.M{
            "$sum": 1,
        },
        "Success": bson.M{"$sum": bson.M{
            "$cond": []interface{}{
                bson.M{ "$eq": []interface{}{"$teststatus", "Pass"}},
                1, 0,
            },
        }},
        "Fail": bson.M{"$sum": bson.M{
            "$cond": []interface{}{
                bson.M{"$eq": []interface{}{"$teststatus", "Fail"}},
                1, 0,
            },
        }},
    },
}

huangapple
  • 本文由 发表于 2017年8月18日 13:21:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/45749030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定