I’m using rad studio (C++) to create simple aggregate across a database.
The database has a bunch of customer info and an array of transactions, all the transactiona have a date.
What I need is only Docs that are within a date period and a count of how many transactions are dated within that period, sounds pretty straight forward.
The aggregation produced by MongoDB compass works perfectly (in compass).
How can I translate the compass aggregate code to use in a TFDMongoPipeline?
The aggregate produced by compass contains 2 stages the first is a “$match” date period – this produces all complying doc’s (this also works in the TFDMongoPipeline using PMatch
Example: PMatch = {“transactions.date”: {$gte: “2024-07-24”,$lte: “2024-08-31” }}
This is working correctly and produces only docs within the date period specified.
However the next stage does the work of counting how many transaction are within the date period and creates a new filed “trans_count”.
Compass produces the following.
{ $addFields: /** * newField: The new field name. * expression: The new field expression. */ { trans_count: { $size: { $filter: { input: "$transactions", as: "trans", cond: { $and: [ { $gte: [ "$$trans.date", "2024-07-24" ] }, { $lte: [ "$$trans.date", "2024-08-31" ] } ] } } } } }
I’ve tried everything i can think of to get the to work in the TDFMongoPipeline with no success.
Everything I’ve tried so far has failed.
Does anyone have experience using TFDMongoPipeline – I’ve found very little to no information regarding it’s use or example code.
Database Docs looks like this:
Customers
cust_id
name
…
transactions:Array
0 Object
trans_no: 123123
date: “2024-07-26”
1 Object
trans_no: 123124
date: “2024-08-03”
I’ve been fighting this for the last 3 days and would really appreciate any help, hints.
I hope I’ve explained this properly and that it makes sense.
Thanks in advance.
Wez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.