如何解决MongoDB聚合$ reduce运算符出错
我最近将Java应用程序从使用MongoDB客户端3升级到了4(集群已经在v4上运行了,终于可以访问该客户端了。)
我的其中一个使用$reduce
运算符的聚合在升级后开始引起错误。
这是服务器发出的错误消息:
'$reduce found an unknown argument: $and'
这是代码段:
List<Bson> agg = Arrays.asList(
unwind("$playlist.songs",new UnwindOptions().preserveNullAndEmptyArrays(true)),project(
fields(
excludeId(),computed("title","$playlist.songs.title"),computed("artist",eq("$reduce",and(
eq("input","$details.playlist.songs.artists.artist_name"),eq("initialValue",""),eq("in",eq("$concat",Arrays.asList("$$value"," ","$$this")))))))),
我最初是使用Mongo Compass工具生成此代码段的,而我尝试使用最新版本(v4)重新生成它,但是它给了我相同的代码段。
我还尝试用简单的$reduce
对象替换Document
参数,但是我开始遇到美元符号和字段名称的问题。如果可能的话,我宁愿继续使用本机聚合助手。
更新:我找到了一种解决方法,即删除and
运算符,并使用纯BSON对象为$reduce
运算符的 value 使用纯本地BSON API :
eq("$reduce",new BsonDocument("input",new BsonString("$details.playlist.songs.artists.artist_name"))
.append("initialValue",new BsonString(""))
.append("in",new BsonDocument("$concat",new BsonArray(Arrays.asList(
new BsonString("$$value"),new BsonString(" "),new BsonString("$$this"))))))))),
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。