Spring Data MongoDB - $eq within $project support

杀马特。学长 韩版系。学妹 提交于 2019-12-11 07:46:45

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/27080374/spring-data-mongodb-eq-within-project-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!