Hi,
I am trying to build two charts with MongoDB Charts, a Number Card and a DataTable. I have two doubts pertaining to this.
1.) Getting the last value for “Equity” from the last document in a collection to display in number card:
Assume I have a time series document of the format where new records are inserted asynchronously at random time intervals
[
{ time: ISODate("22-04-2022T23:00:00Z") , equity : 25100 }
{ time: ISODate("22-04-2022T23:01:00Z") , equity : 25109 }
{ time: ISODate("22-04-2022T23:01:05Z") , equity : 25090 }
.....
]
I would like to print the most recent equity value (25090) in this card. The card should also refresh every 5 secs or 10 secs and display the most recent equity value from the last inserted document. Currently I don’t see how to do this with the aggregation options, as there is no display last value of series option. Perhaps I should write some query to do this? If so, can a MongoDB expert help me with this?
2.) Cummulative sum of profit_csf
column:
The next requirement is to create a new calculated field called balance
which computes the cummulative sum of a profit_csf
column in this datatable below:
<iframe style="background: #21313C;border: none;border-radius: 2px;box-shadow: 0 2px 10px 0 rgba(70, 76, 79, .2);" width="640" height="480" src="https://charts.mongodb.com/charts-project-0-nvgoj/embed/charts?id=62680f50-aa03-494b-8bdf-e57c312d4fe5&maxDataAge=3600&theme=dark&autoRefresh=true"></iframe>
I followed this stackoverflow article but $setWindowFields
is not recognized as valid pipeline operator in MongoDB Charts - Calculated Fields. I also tried:
{ $accumulator: {
sortBy: { time: 1 },
output: {
cumulative: {
$sum: "$profit_csf",
window: { documents: [ "unbounded", "current" ] }
}
}
}}
but, accumulator
is also not accepted in the Atlas Free Tier that i am using. Can you kindly provide a solution to create a new calculated field called Balance that is basically a cummulative sum of the profit_csf
column?
Best Regards,
Dilip