I have a set of trade data that looks like the following
{
"_id" : 1498290900.0,
"trade" : {
"type" : "Adjust",
"data" : {
"type" : "bid",
"rate" : "0.00658714",
"amount" : "3.82354427"
},
"date" : 1498290930291.0,
"name" : "TLX"
}
},{
"_id" : 1498290900.0,
"trade" : {
"type" : "Adjust",
"data" : {
"type" : "ask",
"rate" : "0.00658714",
"amount" : "3.82354427"
},
"date" : 1498290930291.0,
"name" : "TLX"
}
},{
"_id" : 1498290900.0,
"trade" : {
"type" : "Remove",
"data" : {
"type" : "ask",
"rate" : "0.00680891"
},
"date" : 1498290931349.0,
"name" : "TLX"
}
}
These entries are from $rewind
, explaining why the _id
is consistent. My goal now is to group them based on their _id
. So, I attempted the following
{
$group: {
_id: {_id: "$_id", name: "$trade.type", dtype: "$trade.data.type"},
count : {$sum: 1}
},
},{$project: { _id: "$_id._id", type: "$_id.name", count: 1, dtype: "$_id.dtype" } },
{
$group: {
_id: "$_id",
results: { $push : "$$ROOT" }
}
}
This gave me a decent output as shown below
{
"_id" : 1498276800.0,
"results" : [
{
"count" : 16.0,
"_id" : 1498276800.0,
"type" : "Adjust",
"dtype" : "bid"
},
{
"count" : 15.0,
"_id" : 1498276800.0,
"type" : "Remove",
"dtype" : "bid"
},
{
"count" : 3.0,
"_id" : 1498276800.0,
"type" : "Remove",
"dtype" : "ask"
},
{
"count" : 1.0,
"_id" : 1498276800.0,
"type" : "Adjust",
"dtype" : "ask"
}
]
}
However, my aim was to create an output more similar to this
{
"_id" : 1498276800.0,
"Modify": {
"bid":{
"count": 16.0
},
"ask": {
"count": 1.0
}
},
"Remove": {
"bid":{
"count": 15.0
},
"ask": {
"count": 3.0
}
}
}
Despite trying various combinations with $projections
, I couldn't achieve the desired output.
If anyone can guide me in the right direction, it would be greatly appreciated.
Thank you.
UPDATE
Excluding the final stage of the pipeline, here are example documents organized neatly by bid/ask per type and ready to be grouped by _id.
{
"_id" : {
"_id" : 1498276800.0,
"type" : "orderBookRemove"
},
"results" : [
{
"k" : "bid",
"v" : {
"count" : 15.0
}
},
{
"k" : "ask",
"v" : {
"count" : 3.0
}
}
]
},
{
"_id" : {
"_id" : 1498276800.0,
"type" : "orderBookModify"
},
"results" : [
{
"k" : "bid",
"v" : {
"count" : 16.0
}
},
{
"k" : "ask",
"v" : {
"count" : 1.0
}
}
]
}
When the last part of the pipeline is executed, i.e.,
{ "$group": {
"_id": "$_id._id",
"results": {
"$push": {
"k": "$_id.type",
"v": "$results"
}
}
}}
I only receive the first 'bid' element of the results array. The second item 'ask' seems to disappear?
{
"_id" : 1498280700.0,
"results" : [
{
"k" : "orderBookRemove",
"v" : [
{
"k" : "bid",
"v" : {
"count" : 9.0
}
}
]
},
{
"k" : "orderBookModify",
"v" : [
{
"k" : "bid",
"v" : {
"count" : 6.0
}
}
]
}
]
}