Spring数据MongoDB - $ eq内$项目支持 [英] Spring Data MongoDB - $eq within $project support

查看:1064
本文介绍了Spring数据MongoDB - $ eq内$项目支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在我的Spring项目中编写一个聚合查询MongoDB,其中我使用$ project运算符。在这个操作符中,我想比较两个字段,以返回结果作为投影的匹配键值。这里是mongoDB shell等效(它的工作原理):

{$ project:
{matches:
{$ eq:[$ lastDate $ meta.date]}
}
}



我阅读了Spring Data MongoDB文档,有用的信息关于ProjectionOperator的'andExpression'方法使用SpEL。我调查的结果Java代码是:

new ProjectionOperation()。andExpression(lastDate == meta.date)。as(matches)



不幸的是我收到了异常:

java.lang.IllegalArgumentException:不支持的元素:
org.springframework.data.mongodb.core.spel.OperatorNode@70c1152a类型:class org.springframework.data.mongodb.core.spel.OperatorNode您的SpEL表达式中可能有语法错误!



就我已经检查,Spring Data MongoDB正确处理所有算术运算符,但不能处理比​​较。因此,我想问的是有什么其他方法来创建这样的查询与Spring Data MongoDB?

解决方案

我通过传递JSON聚合命令(使用DBObjects创建)解决了这个问题为了保持查询的灵活性)到MongoDB,即:

MongoOperations#executeCommand(DBObject command)


I'm currently writing an aggregation query for MongoDB in my Spring project in which I'm using $project operator. Within this operator I would like to compare two fields in order to return the result as projected "matches" key value. Here's the mongoDB shell equivalent (which works): {$project: {matches: {$eq: ["$lastDate", "$meta.date"]} } }

I've read Spring Data MongoDB documentation and found some useful info about ProjectionOperator's 'andExpression' method which uses SpEL. The result Java code of my investigation was: new ProjectionOperation().andExpression("lastDate == meta.date").as("matches")

Unfortunately I'm receiving exception: java.lang.IllegalArgumentException: Unsupported Element: org.springframework.data.mongodb.core.spel.OperatorNode@70c1152a Type: class org.springframework.data.mongodb.core.spel.OperatorNode You probably have a syntax error in your SpEL expression!

As far as I've checked, Spring Data MongoDB handles all Arithmetic operators correctly but cannot handle the comparison ones. Therefore I want to ask is there any other way to create such query with Spring Data MongoDB? Or maybe I don't know something crucial about SpEL?

解决方案

I resolved this issue by passing JSON aggregate command (created with DBObjects in order to preserve flexibility of the query) to MongoDB, i.e.: MongoOperations#executeCommand(DBObject command)

这篇关于Spring数据MongoDB - $ eq内$项目支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆