问题描述
我最近将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 (将#修改为@)