odrilon
(Ory Drilon)
9
I examined the code of MqlExpression (the main implementation of MqlValue) and there really is a feature gap in the MongoDB Java API/library where it doesn’t support $variable (unless I misunderstood the code).
You can work around this by doing either of the following:
(1) Have a previous stage add the $variable as a field. I think this this is a peformance penalty if you’re are using the $variable in an early $match that filters out many records, because now MongoDB will need to process all the records before filtering them out.
(2) Manually create the expression by making a Document, as @steevej usually does. Thankfully, MqlValue instances get converted into Bson when you place them into Document instances, so you can do something like below if you’re using the MqlValue API in most of your code.
final MqlValue field = getThisFromSomewhere();
final Document document = new Document(Map.of(
"$eq", List.of(field, "$$variable")
));
1 Like