微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

使用Spring Data的Projectionoperation类在MongoDB上创建聚合查询时,使用带有“ $”(例如’test $’)字符的字段会导致IllegalArgumentException

验证spring数据mongodb源时,我注意到在AggregationField类的构造函数中,对字段名称进行了清理. 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 Data问题吗?

最佳答案
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 Data无法支持功能,除非对此提供支持,以使其成为正式的Java驱动程序.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐