为什么 Spring Data MongoDB 拒绝聚合管道中包含 $ 的字段名称?

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

使用 Spring 数据的 ProjectionOperation class 在 MongoDB 上创建聚合查询时,将字段与 "$"(例如 'test$')字符一起使用会导致 IllegalArgumentException

验证 spring 数据 mongodb 来源,我注意到在 AggregationField class 的构造函数中清理了字段名称被执行。 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);
}

MongoDB 中的字段命名不鼓励使用“$”字符,或者这是 Spring 数据问题?

这似乎是一个 Spring 数据 Mongodb 问题...您的字段名称应该是允许的。

You can use any (UTF8) character in the field name which aren't special (contains ".", or starts with "$").

来源: SERVER-3229

您可以尝试使用 BasicDBObject. Here is one example:

构建您的项目操作

官方MongoDB驱动目前不支持的reference documentation clearly states

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}

即Spring 在官方 Java 驱动程序支持之前,数据无法支持它。