gpt4 book ai didi

相当于 PostsgreSQL 的 MongoDB 聚合查询

转载 作者:可可西里 更新时间:2023-11-01 10:05:40 25 4
gpt4 key购买 nike

这个问题有两个部分。集合结构为:

_id: MongoID,
agent_id: 字符串,
结果:字符串,
创建时间:ISO 日期,
...其他领域...

第一部分:
期望的输出:每个 agent_id 的一个结果和带有计数的结果组合:使用 PostgreSQL 的具有等效 SQL 的 TUPLE 表示。

( "1234", "Success", 4 ),
( "1234", "Failure", 4 ),
( "4567", "Success", 3 ),
( "7896", "Failure", 2 ),
.....

SELECT agent_id, result, count(*)
FROM table
GROUP BY agent_id, result
HAVING created_on >= now()::date;

我想出了下面的 mongo 查询....我想我有一个概念或语法错误。文档说要使用 $match early in the pipeline: ,但是虽然 $match 在我自己运行时限制了查询,但只要我添加 $group 我就会得到很多结果。此外,我似乎无法理解如何按多个字段进行分组。如何编辑下面的查询以获得类似于上面的 SQL 查询的结果?

db.collection.aggregate(
{ $match :
{ created_on:
{ $gte: new Date('08-13-2012') //some arbitrary date
}
}, $group:
{ _id:"$agent_id" },
$project:
{_id:0, agent_id:1, result:1}
})

第 2 部分)第一个结果集是足够的,但不是最佳的。使用 PostgreSQL,我可以获得如下结果集:

( "1234", { "Success", "Failure" }, { 4, 3 } ),
( "4567", { "Success", "Failure" }, { 3, 0 } ),
( "7896", { "Success", "Failure" }, { 0, 2 } )

我可以在 Postgresql 中使用数组数据类型和 set_to_array 函数(自定义函数)执行此操作。 Pg 特定的 SQL 是:

SELECT agent_id, set_to_array(result), set_to_array( count(*) )
FROM table
GROUP BY agent_id, result
HAVING created_on >= now()::date;

我相信 mongodb 中的等效数据结构如下所示:

[
{ "1234", [ { "success": 4 }, { "failure": 4 } ] },
{ "4567", [ { "success": 3 }, { "failure": 0 } ] },
{ "7896", [ { "success": 0 }, { "failure": 0 } ] }
]

是否可以使用 mongodb 聚合框架实现这些所需的压缩结果?

最佳答案

给你:

创建了一些测试数据:

db.test.insert({agent_id:"1234", result:"Failure", created_on:new Date()}); db.test.insert({agent_id:"1234", result:"Success", created_on:new Date()}); db.test.insert({agent_id:"1234", result:"Failure", created_on:new Date()}); db.test.insert({agent_id:"1234", result:"Success", created_on:new Date()}); db.test.insert({agent_id:"1234", result:"Failure", created_on:new Date()}); db.test.insert({agent_id:"1234", result:"Success", created_on:new Date()}); db.test.insert({agent_id:"1234", result:"Success", created_on:new Date()}); db.test.insert({agent_id:"1324", result:"Success", created_on:new Date()}); db.test.insert({agent_id:"1324", result:"Success", created_on:new Date()}); db.test.insert({agent_id:"1324", result:"Success", created_on:new Date()}); db.test.insert({agent_id:"1324", result:"Success", created_on:new Date()}); db.test.insert({agent_id:"1324", result:"Failure", created_on:new Date()}); db.test.insert({agent_id:"1324", result:"Failure", created_on:new Date()}); db.test.insert({agent_id:"1324", result:"Failure", created_on:new Date()}); db.test.insert({agent_id:"1324", result:"Failure", created_on:new Date()}); db.test.insert({agent_id:"1324", result:"Failure", created_on:new Date()}); db.test.insert({agent_id:"1324", result:"Failure", created_on:new Date()}); db.test.insert({agent_id:"1324", result:"Failure", created_on:new Date()});

db.test.aggregate(
{
$match:{ /* filter out the things you want to aggregate */
created_on:{$gte:new Date(1000000)}
}
},
{
$group: {_
_id: { /* the things you want to group on go in the _id */
agent_id:"$agent_id",
result:"$result"
},
count:{$sum:1} /* simple count */
}
},
{
$project: { /* take the id out into the separate fields for your tuple. */
_id:0,
agent_id:"$_id.agent_id",
result:"$_id.result",
count:"$count"
}
});

给予:

{
"result" : [
{
"count" : 7,
"agent_id" : "1324",
"result" : "Failure"
},
{
"count" : 4,
"agent_id" : "1324",
"result" : "Success"
},
{
"count" : 4,
"agent_id" : "1234",
"result" : "Success"
},
{
"count" : 3,
"agent_id" : "1234",
"result" : "Failure"
}
],
"ok" : 1
}

添加第 2 部分——与第 1 部分非常相似,但计数有点复杂;基本上,只有当它与您想要计算的相匹配时,您才计算:

db.test.aggregate(
{
$match: {
created_on: {$gte:new Date(1000000)}
}
},
{
$group: {
_id: {
agent_id:"$agent_id"
},
failure: {
$sum:{
$cond:[
{$eq:["$result","Failure"]},
1,
0
]
}
},
success: {
$sum: {
$cond:[
{$eq:["$result","Success"]},
1,
0
]
}
}
}
},
{
$project: {
_id: 0,
agent_id: "$_id.agent_id",
failure: "$failure",
success: "$success"
}
});

给予:

{
"result" : [
{
"failure" : 7,
"success" : 4,
"agent_id" : "1324"
},
{
"failure" : 3,
"success" : 4,
"agent_id" : "1234"
}
],
"ok" : 1
}

关于相当于 PostsgreSQL 的 MongoDB 聚合查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12700836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com