I never would have guessed it was possible like that. Out of sheer curiosity, could you explain how that works? Is it taking advantage of the way computed/eq/and are represented by the java driver to use a projection/aggregation not “natively” supported by the higher-level API?
Is it taking advantage of the way computed/eq/and are represented by the java driver to use a projection/aggregation
Yes, using the driver and its API calls.
I am using MongoDB Java driver APIs to build the Java code to connect to the MongoDB database server, build and run the aggregation query (thats the only way, as I know, to access the MongoDB database from Java).
In general, if you are running the aggregation from the mongo shell, there is a mongo shell’s associated driver. All client programs, like mongo shell or Java or Python programs/scripts, access MongoDB via a driver and are coded using relevant APIs.
I wasn’t really asking that, though thank you for the answer, I was curious how you seem to be manually creating the bson query object using “eq” and “and” in the java code, specifically. I looked for how to do this a decent amount, and e.g the docs for projections for the java driver don’t even mention the $filter operator: Projections
I’d been looking for something like Projections.filter, but apparently since it’s not there it required manually building the bson query. If I understand correctly, the “eq” filter is being used/abused to represent arbitrary key-value pairs, and the “and” filter to represent arbitrary bson objects, allowing you to manually construct the query? Without using the higher level constructs from Aggregates.* or Projections.*
Your solution worked perfectly, and if I understand the way you created the bson query manually using “eq” and “and” correctly I should be able to do the same for anything else I find missing in the “dumb” top-level api. Thank you!
I looked for how to do this a decent amount, and e.g the docs for projections for the java driver don’t even mention the $filter operator…
There is no specific API for the $filter aggregation array operator; I haven’t seen one yet. If you are looking for such constructs the Spring MongoDB APIs have those; e.g using the ArrayOperators.Filter:
The following aggregation stage with $project and $filter,