这个问题有两个部分。收集结构是:MongoDB聚合查询相当于PostsgreSQL
_id:MongoID,
AGENT_ID:字符串,
结果:字符串,
created_on: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查询....我想我有一个概念或语法错误。该文档声称使用$匹配early in the pipeline:,但尽管$匹配限制了查询,当我自己运行它时,只要添加$组,我就可以获得许多结果。另外,我似乎无法理解如何组合多个领域。我如何编辑下面的查询来获得像上面的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 })
我可与阵列数据类型和函数set_to_array(自定义功能)为此在PostgreSQL。在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的总框架,这些期望的压缩结果?
是否有可能放松,以得到一个“总计”?我能够为每个结果添加小计(失败和成功的总和),但我不确定是否生成一条记录,可以在查询本身中总结所有代理的成功和失败。 – Ketema
你的意思是在同一个查询中,还是在一个新的查询中? –
如果结果可能在同一个查询中有一个总计,那么它会很有趣,但不知道这是否可能。 – Ketema