Java 如何在mongoDB中使用morphia进行分组和计数?
我将Morphia与Java中的MongoDB一起使用,我有这样的集合Java 如何在mongoDB中使用morphia进行分组和计数?,java,mongodb,morphia,Java,Mongodb,Morphia,我将Morphia与Java中的MongoDB一起使用,我有这样的集合 {"_id":"5d5e7ce7869eef030869e85c", "ip":"66.249.79.181", "date":"2019-08-19T18:30:00.000Z", "request_url":"https://www.example.com/home", "status_code":"200", "bot":"Google Android", "type
{"_id":"5d5e7ce7869eef030869e85c",
"ip":"66.249.79.181",
"date":"2019-08-19T18:30:00.000Z",
"request_url":"https://www.example.com/home",
"status_code":"200",
"bot":"Google Android",
"type":"type/html",
"domain":"https://www.example.com"},
{"_id":"5d5e7ce7869eef030869e85c",
"ip":"66.249.79.181",
"date":"2019-08-19T18:30:00.000Z",
"request_url":"https://www.example.com/home",
"status_code":"200",
"bot":"Google",
"type":"type/html",
"domain":"https://www.example.com"},
{"_id":"5d5e7ce7869eef030869e85c",
"ip":"66.249.79.181",
"date":"2019-08-19T18:30:00.000Z",
"request_url":"https://www.example.com/home",
"status_code":"200",
"bot":"bing",
"type":"type/html",
"domain":"https://www.example.com"}
{"request_url":"https://www.example.com/home",
"status_code":"200",
"Google": 1,
"Google Android": 1,
"bing": 1,
"type":"type/html", }
我需要使用分组(“request_url”)并获得“bot”字段的计数,如果我需要这样的结果该怎么办
{"_id":"5d5e7ce7869eef030869e85c",
"ip":"66.249.79.181",
"date":"2019-08-19T18:30:00.000Z",
"request_url":"https://www.example.com/home",
"status_code":"200",
"bot":"Google Android",
"type":"type/html",
"domain":"https://www.example.com"},
{"_id":"5d5e7ce7869eef030869e85c",
"ip":"66.249.79.181",
"date":"2019-08-19T18:30:00.000Z",
"request_url":"https://www.example.com/home",
"status_code":"200",
"bot":"Google",
"type":"type/html",
"domain":"https://www.example.com"},
{"_id":"5d5e7ce7869eef030869e85c",
"ip":"66.249.79.181",
"date":"2019-08-19T18:30:00.000Z",
"request_url":"https://www.example.com/home",
"status_code":"200",
"bot":"bing",
"type":"type/html",
"domain":"https://www.example.com"}
{"request_url":"https://www.example.com/home",
"status_code":"200",
"Google": 1,
"Google Android": 1,
"bing": 1,
"type":"type/html", }
如何通过“request_url”字段进行分组,并使用下面的聚合获取每个“bot”字段的计数:
db.collection.aggregate([
{
$group: {
_id: {
request_url: "$request_url",
bot: "$bot"
},
type: {
$max: "$type"
},
status_code: {
$max: "$status_code"
},
count: {
$sum: 1
}
}
},
{
$group: {
_id: "$_id.request_url",
type: {
$max: "$type"
},
status_code: {
$max: "$status_code"
},
counts: {
$push: {
bot: "$_id.bot",
count: "$count"
}
}
}
}
])
提供投入:
[
{
"ip": "66.249.79.181",
"date": "2019-08-19T18:30:00.000Z",
"request_url": "https://www.example.com/home",
"status_code": "200",
"bot": "Google Android",
"type": "type/html",
"domain": "https://www.example.com"
},
{
"ip": "66.249.79.181",
"date": "2019-08-19T18:30:00.000Z",
"request_url": "https://www.example.com/home",
"status_code": "200",
"bot": "Google",
"type": "type/html",
"domain": "https://www.example.com"
},
{
"ip": "66.249.79.181",
"date": "2019-08-19T18:30:00.000Z",
"request_url": "https://www.example.com/home",
"status_code": "200",
"bot": "bing",
"type": "type/html",
"domain": "https://www.example.com"
}
]
输出为:
[
{
"_id": "https://www.example.com/home",
"counts": [
{
"bot": "bing",
"count": 1
},
{
"bot": "Google",
"count": 1
},
{
"bot": "Google Android",
"count": 1
}
],
"status_code": "200",
"type": "type/html"
}
]
以下查询可以获得预期的输出:
db.collection.aggregate([
{
$group:{
"_id":{
"request_url":"$request_url",
"bot":"$bot"
},
"request_url":{
$first:"$request_url"
},
"k":{
$first:"$bot"
},
"v":{
$sum:1
}
}
},
{
$group:{
"_id":"$request_url",
"request_url":{
$first:"$request_url"
},
"bots":{
$push:{
"k":"$k",
"v":"$v"
}
}
}
},
{
$project:{
"info.request_url":"$request_url",
"bots":{
$arrayToObject:"$bots"
}
}
},
{
$project:{
"info":{
$mergeObjects:["$info","$bots"]
}
}
},
{
$replaceRoot:{
newRoot:"$info"
}
}
]).pretty()
数据集:
{
"_id" : ObjectId("5d6d0f456bc2ad3b23f7dfcf"),
"ip" : "66.249.79.181",
"date" : "2019-08-19T18:30:00.000Z",
"request_url" : "https://www.example.com/home",
"status_code" : "200",
"bot" : "Google Android",
"type" : "type/html",
"domain" : "https://www.example.com"
}
{
"_id" : ObjectId("5d6d0f456bc2ad3b23f7dfd0"),
"ip" : "66.249.79.181",
"date" : "2019-08-19T18:30:00.000Z",
"request_url" : "https://www.example.com/home",
"status_code" : "200",
"bot" : "Google",
"type" : "type/html",
"domain" : "https://www.example.com"
}
{
"_id" : ObjectId("5d6d0f456bc2ad3b23f7dfd1"),
"ip" : "66.249.79.181",
"date" : "2019-08-19T18:30:00.000Z",
"request_url" : "https://www.example.com/home",
"status_code" : "200",
"bot" : "bing",
"type" : "type/html",
"domain" : "https://www.example.com"
}
{
"request_url" : "https://www.example.com/home",
"bing" : 1,
"Google" : 1,
"Google Android" : 1
}
输出:
{
"_id" : ObjectId("5d6d0f456bc2ad3b23f7dfcf"),
"ip" : "66.249.79.181",
"date" : "2019-08-19T18:30:00.000Z",
"request_url" : "https://www.example.com/home",
"status_code" : "200",
"bot" : "Google Android",
"type" : "type/html",
"domain" : "https://www.example.com"
}
{
"_id" : ObjectId("5d6d0f456bc2ad3b23f7dfd0"),
"ip" : "66.249.79.181",
"date" : "2019-08-19T18:30:00.000Z",
"request_url" : "https://www.example.com/home",
"status_code" : "200",
"bot" : "Google",
"type" : "type/html",
"domain" : "https://www.example.com"
}
{
"_id" : ObjectId("5d6d0f456bc2ad3b23f7dfd1"),
"ip" : "66.249.79.181",
"date" : "2019-08-19T18:30:00.000Z",
"request_url" : "https://www.example.com/home",
"status_code" : "200",
"bot" : "bing",
"type" : "type/html",
"domain" : "https://www.example.com"
}
{
"request_url" : "https://www.example.com/home",
"bing" : 1,
"Google" : 1,
"Google Android" : 1
}
说明:数据在请求url上分组,并计算不同的键值对。键(k)将保存bot名称,值(v)将保存发生计数。随后,将每一对放入一个数组,然后将数组转换为一个对象。他指定了“使用morphia”。他指定了“使用morphia”。