我正在尝试在Grails项目中将groupProperty与MongoDB一起使用。 我使用的是Grails的2.3.2版本和MongoDB插件的3.0.1版本。
我的标准如下:
def groupedScenarios = Scenario.withCriteria { eq 'scenarioVersion', { projections { max 'scenarioVersion' } } projections { groupProperty 'scenarioNumber' } }我正在尝试找到scenarioVersion对于给定scenarioNumber最高的场景。 我甚至不知道我的标准是否正确,因此我对我的最终目标的解释。
返回的错误是
No signature of method: <my service>.groupProperty() is applicable for argument types: (java.lang.String) values: [scenarioNumber] Possible solutions: getProperty(java.lang.String), hasProperty(java.lang.String). Stacktrace follows: Message: No signature of method: <my service>.groupProperty() is applicable for argument types: (java.lang.String) values: [scenarioNumber] Possible solutions: getProperty(java.lang.String), hasProperty(java.lang.String)有些搜索建议不支持groupProperty,但插件版本6的文档建议现在使用。
我的标准有问题吗? 如果这是一个插件版本问题,有没有人知道Grails MongoDB插件何时支持groupProperty?
我真的很难找到关于以前版本的Grails插件的文档 - 我错过了什么吗? 据我所知, https ://www.grails.org/plugin/mongodb上的插件页面不允许我查看旧版本。 真的很沮丧。
I'm trying to use groupProperty with MongoDB in a Grails project. I'm on version 2.3.2 of Grails and version 3.0.1 of the MongoDB plugin.
My criteria looks like this:
def groupedScenarios = Scenario.withCriteria { eq 'scenarioVersion', { projections { max 'scenarioVersion' } } projections { groupProperty 'scenarioNumber' } }I'm trying to find the scenarios where the scenarioVersion is highest for a given scenarioNumber. I don't even know if my criteria are correct yet, hence my explanation of my end goal.
The error returned is
No signature of method: <my service>.groupProperty() is applicable for argument types: (java.lang.String) values: [scenarioNumber] Possible solutions: getProperty(java.lang.String), hasProperty(java.lang.String). Stacktrace follows: Message: No signature of method: <my service>.groupProperty() is applicable for argument types: (java.lang.String) values: [scenarioNumber] Possible solutions: getProperty(java.lang.String), hasProperty(java.lang.String)Some searching has suggested groupProperty wasn't supported, but the docs for version 6 of the plugin suggest it is now.
Am I doing anything wrong with my criteria? If it's a plugin version issue, does anyone know when groupProperty became supported by the Grails MongoDB plugin?
I've really struggled to find documentation about previous versions of Grails plugins - am I missing something? The plugin page at https://www.grails.org/plugin/mongodb doesn't allow me to look at old releases as far as I can tell. Really frustrating.
最满意答案
您应该使用“mongo的本机”查询语法来执行此类任务。
根据我的经验,GORM for mongo适用于简单的动态查找器,它可以在任何数据库上以相同的方式工作,但是如果需要触发特定于数据库的查询,则必须使用本机,因为GORM criteria太偏向于SQL。
所以,你的查询可能看起来像:
def groupedScenarios = Scenario.collection.aggregate( [ $group:[ _id:'_id', maxNumber:[ $max:scenarioNumber ] ] ] ).results()我不是mongo专家,所以你必须自己组装一个正确的查询,根据mongo ref doc
You should be using "mongo's native" query syntax for such tasks.
From my experience GORM for mongo is fine for simple dynamic-finders which work the same way on any DB out there, but if you need to fire a DB-specific query you have to go native, as GORM criteria is too SQL-biased.
So, you query could look something like:
def groupedScenarios = Scenario.collection.aggregate( [ $group:[ _id:'_id', maxNumber:[ $max:scenarioNumber ] ] ] ).results()I'm not a mongo-expert, so you have to assemble a proper query yourself, as per mongo ref doc
更多推荐
发布评论