我正在尝试查找一个mongoDB脚本,该脚本将查看一个集合,其中存在多个同一文档的记录,并且仅向我提供每个文档的最新版本作为结果集。
我无法用英语解释它比上面更好但是下面的这个小SQL可能会进一步解释它。我想要transaction_reference
每个文档,但只需要最新的日期版本(object_creation_date
)。
select
t.transaction_reference,
t.transaction_date,
t.object_creation_date,
t.transaction_sale_value
from MyTable t
inner join (
select
transaction_reference,
max(object_creation_date) as MaxDate
from MyTable
group by transaction_reference
) tm
on t.transaction_reference = tm.transaction_reference
and t.object_creation_date = tm.MaxDat
同一文档有多个版本的原因是因为我想存储事务的每次迭代。我第一次收到一份文件,可能是在联合国教科文组织的transaction_status
,然后我再次收到同一笔交易,这次transaction_status
是付费的。
一些分析将是SUM所有唯一交易,而其他一些分析可能是衡量状态为UNPAID的文件与下一个PAID之间的时间距离。
根据要求,这里有两个文件:
{
"_id": {
"$oid": "579aa337f36d2808839a05e8"
},
"object_class": "Goods & Services Transaction",
"object_category": "Revenue",
"object_type": "Transaction",
"object_origin": "Sage One",
"object_origin_category": "Bookkeeping",
"object_creation_date": "2016-07-05T00:00:00.201Z",
"party_uuid": "dfa1e80a-5521-11e6-beb8-9e71128cae77",
"connection_uuid": "b945bd7c-7988-4d2a-92f5-8b50ab218e00",
"transaction_reference": "SI-1",
"transaction_status": "UNPAID",
"transaction_date": "2016-06-16T00:00:00.201Z",
"transaction_due_date": "2016-07-15T00:00:00.201Z",
"transaction_currency": "GBP",
"goods_and_services": [
{
"item_identifier": "PROD01",
"item_name": "Product One",
"item_quantity": 1,
"item_gross_unit_sale_value": 1800,
"item_revenue_category": "Sales Revenue",
"item_net_unit_cost_value": null,
"item_net_unit_sale_value": 1500,
"item_unit_tax_value": 300,
"item_net_total_sale_value": 1500,
"item_gross_total_sale_value": 1800,
"item_tax_value": 300
}
],
"transaction_gross_value": 1800,
"transaction_gross_curr_value": 1800,
"transaction_net_value": 1500,
"transaction_cost_value": null,
"transaction_payments_value": null,
"transaction_payment_extras_value": null,
"transaction_tax_value": 300,
"party": {
"customer": {
"customer_identifier": "11",
"customer_name": "KP"
}
}
}
和现在付款的第二个版本
{
"_id": {
"$oid": "579aa387f36d2808839a05ee"
},
"object_class": "Goods & Services Transaction",
"object_category": "Revenue",
"object_type": "Transaction",
"object_origin": "Sage One",
"object_origin_category": "Bookkeeping",
"object_creation_date": "2016-07-16T00:00:00.201Z",
"party_uuid": "dfa1e80a-5521-11e6-beb8-9e71128cae77",
"connection_uuid": "b945bd7c-7988-4d2a-92f5-8b50ab218e00",
"transaction_reference": "SI-1",
"transaction_status": "PAID",
"transaction_date": "2016-06-16T00:00:00.201Z",
"transaction_due_date": "2016-07-15T00:00:00.201Z",
"transaction_currency": "GBP",
"goods_and_services": [
{
"item_identifier": "PROD01",
"item_name": "Product One",
"item_quantity": 1,
"item_gross_unit_sale_value": 1800,
"item_revenue_category": "Sales Revenue",
"item_net_unit_cost_value": null,
"item_net_unit_sale_value": 1500,
"item_unit_tax_value": 300,
"item_net_total_sale_value": 1500,
"item_gross_total_sale_value": 1800,
"item_tax_value": 300
}
],
"transaction_gross_value": 1800,
"transaction_gross_curr_value": 1800,
"transaction_net_value": 1500,
"transaction_cost_value": null,
"transaction_payments_value": null,
"transaction_payment_extras_value": null,
"transaction_tax_value": 300,
"party": {
"customer": {
"customer_identifier": "11",
"customer_name": "KP"
}
}
}
感谢您的支持,Matt
答案 0 :(得分:1)
如果我正确理解了这个问题,你可以使用类似的东西
db.getCollection('yourTransactionsCollection').aggregate([
{
$sort: {
"transaction_reference": 1,
"object_creation_date": -1
}
},
{
$group: {
_id: "$transaction_reference",
"transaction_date": { $first: "$transaction_date" },
"object_creation_date": { $first: "$transaction_date" },
"transaction_sale_value": { $first: "$transaction_sale_value" }
}
}
])
输出如下结果
{
"_id" : "SI-1",
"transaction_date" : "2016-06-16T00:00:00.201Z",
"object_creation_date" : "2016-06-16T00:00:00.201Z",
"transaction_sale_value" : null
}
请注意,您可以将$sort
更改为仅包含object_creation_date
,但我同时包含transaction_reference
和object_creation_date
,因为我认为创建综合索引是有意义的在他们两个而不仅仅是创建日期。根据您的索引进行调整,以便$sort
匹配一个
此外,结果中没有文档字段transaction_sale_value
因此null
。也许你错过了它,或者它只是不在你的样本文件中,但我认为你明白并且可以根据你的需要进行调整。