Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Spring Data MongoDB reject a field name containing a $ in an aggregation pipeline?

When creating an aggregate query on MongoDB using Spring Data's ProjectionOperation class, using field with the "$" (for example 'test$') character causes an IllegalArgumentException

Verifying the spring data mongodb sources, I noticed that in the constructor of the AggregationField class a cleanup of the field name is performed. Fields.java Class

private static String cleanUp(String source) {

    if (Aggregation.SystemVariable.isReferingToSystemVariable(source)) {
        return source;
    }

    int dollarIndex = source.lastIndexOf('$');
    return dollarIndex == -1 ? source : source.substring(dollarIndex + 1);
}

The nomenclature of the fields in MongoDB discourage the use of the "$" character or this is a Spring Data problem?

like image 580
Marco Avatar asked Dec 08 '25 10:12

Marco


1 Answers

The reference documentation clearly states that it's currently not supported by the official MongoDB drivers:

IMPORTANT

The MongoDB Query Language cannot always meaningfully express queries over documents whose field names contain these characters (see SERVER-30575). Until support is added in the query language, the use of $ and . in field names is not recommended and is not supported by the official MongoDB drivers. {quote}

I.e. Spring Data cannot support that until support for that makes it into the official Java driver.

like image 96
Oliver Drotbohm Avatar answered Dec 09 '25 23:12

Oliver Drotbohm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!