0

我在返回预期结果的 Grails 控制器中有以下代码:

def alm = SiteHistory.collection.find([
   "phoneNumber":"${params.phoneNumber}",
   "fields.P2Running":true])
   .limit(20).toArray()

但是,我想根据输入参数修改搜索。当 params.alarm == "P2Running" 时,以下查询不返回任何文档

def fieldsSelect = "fields."+params.alarm
def alm = SiteHistory.collection.find([
   "phoneNumber":"${params.phoneNumber}",
   fieldSelect:true])
   .limit(20).toArray()

我也试过:

def fieldsSelect = "fields."+params.alarm
def alm = SiteHistory.collection.find([
   "phoneNumber":"${params.phoneNumber}",
   "${fieldSelect}":true])
   .limit(20).toArray()

产生

Message: org.codehaus.groovy.runtime.GStringImpl cannot be cast to java.lang.String
    at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:134)
    at org.bson.BasicBSONEncoder.putObject(BasicBSONEncoder.java:86)
    at com.mongodb.DefaultDBEncoder.writeObject(DefaultDBEncoder.java:27)
    at com.mongodb.OutMessage.putObject(OutMessage.java:142)
    at com.mongodb.OutMessage._appendQuery(OutMessage.java:85)
    at com.mongodb.OutMessage.query(OutMessage.java:44)
    at com.mongodb.OutMessage.query(OutMessage.java:38)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:303)
    at com.mongodb.DBCursor._check(DBCursor.java:369)
    at com.mongodb.DBCursor._hasNext(DBCursor.java:498)
    at com.mongodb.DBCursor._fill(DBCursor.java:558)
    at com.mongodb.DBCursor.toArray(DBCursor.java:596)
    at com.mongodb.DBCursor.toArray(DBCursor.java:584)
    at remote.SiteHistoryController$_closure2$$EO0HJb0Z.doCall(SiteHistoryController.groovy:58)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)

我的域对象是

class SiteHistory {
    ObjectId id
    String phoneNumber
    Date receiveDate=null
    Map<String,Object> fields = [:]
    static mapWith = "mongo"
}

在添加数据之前,不会知道字段中的字符串键。

一个示例文档是:

{
    "_id" : ObjectId("513eaefe290b3700b8e2c966"),
    "phoneNumber" : "1234567890",
    "receiveDate" : "Wed Mar 06 2013 23:52:47 GMT-0500 (EST)",
    "fields" : {
            "P1Runtime" : 37.6,
            "P1Starts" : 23,
            "P1Running" : false,
            "P2Runtime" : 35.7,
            "P2Starts" : 25,
            "P2Running" : true,
            "WetWellLvl" : 10,
            "HighLvlAlarm" : false
    }
}

任何想法为什么从参数创建的字符串不起作用?

4

1 回答 1

0

以下是适用于 Grails 2.0.1 和 Mongodb 1.0.0.RC4 的代码:

def query = new BasicDBObject("phoneNumber", params.phoneNumber)
    .append("fields.${params.alarm}", true)

def alm = SiteHistory.collection.find(query).limit(20).toArray()        

我没有尝试使用标准,因为我在某处读到它们不适用于 mongodb 对象。但是,我确实有在此对象上工作的命名查询。所以我尝试了一个标准并且它有效!(注意:它适用于 Grails 2.2.1 和 mongodb 1.2.0。)

这是代码:

def c = SiteHistory.createCriteria()
def alm = c.list (max:20, offset:0) {
    eq ("phoneNumber", params.phoneNumber)
    eq ("fields.${params.alarm}", true)
}

我在使用 Grails 2.2.1 时遇到了其他问题,所以我不会升级。

于 2013-03-19T23:54:21.593 回答