I have a collection that consists of documents of this structure:
{
_id:"qZAorHhOUc0d7fU6Qip1X3Hs"
from_address:"0x60b86af869f23aeb552fb7f3cabd11b829f6ab2f"
hash:"0xc8fc2159cf45831c3ca39422bab75dd325aafae6b932d5fbd10ef68f1d6e27b6"
to_address:"0x4cb18386e5d1f34dc6eea834bf3534a970a3f8e7"
_created_at:2022-04-06T12:10:49.527 00:00
_updated_at:2022-04-06T12:13:45.705 00:00
block_hash:"0x4a1414b91c6eeb717af5108eb6eb68cb4956181bb9cf34fd7e0232c8836852b3"
block_number:14532271
block_timestamp:2022-04-06T12:10:40.000 00:00
confirmed:true
decimal:4.788E-14
gas:900000
gas_price:39753448498
input:"0x1cff79cd000000000000000000000000cd442c217a1c8334972cb98ef0ab6c765402..."
nonce:46022
receipt_cumulative_gas_used:276298
receipt_gas_used:125211
receipt_status:1
transaction_index:1
value:"47880"
}
I want an aggregation that displays for each "from_address" how many transactions to another address have been made.
For example
{ from_address1, to_address1, transactions: sum of transactions from address1 to to_address1 }
I started by trying to group by from_address and to_address but I am stuck on how to proceed from there.
{
_id: {from_address:'$from_address',
to_address:'$to_address'
},
}
This gives me all unique transactions from A to B, how would I count now "from A to B made x transactions to B"?
CodePudding user response:
Need $sum
operator.
db.collection.aggregate([
{
$group: {
_id: {
from_address: "$from_address",
to_address: "$to_address"
},
count: {
$sum: 1
}
}
}
])