Mongodb GrailsMongo低级API

Mongodb GrailsMongo低级API,mongodb,grails,Mongodb,Grails,当使用Mongo低级API时,groovy代码中这一行的等价物是什么 db.countrycodes.findOne({"Country":"Antarctica"}) 这一行在Mongo shell中成功地为我找到了合适的记录,但我在我的控制器方法中尝试了它的许多变体,并且不断得到NULL。以下是我目前的尝试,但失败了: MongoClient mongoClient = new MongoClient("localhost", 27017) DB db = mongoCl

当使用Mongo低级API时,groovy代码中这一行的等价物是什么

 db.countrycodes.findOne({"Country":"Antarctica"})
这一行在Mongo shell中成功地为我找到了合适的记录,但我在我的控制器方法中尝试了它的许多变体,并且不断得到NULL。以下是我目前的尝试,但失败了:

    MongoClient mongoClient = new MongoClient("localhost", 27017)
    DB db = mongoClient.getDB("twcdb");
    DBCollection coll = db.getCollection('countrycodes')
    println coll.find("Country":"Antarctica")
我知道我的集合和db是非空的,因为当我执行
find()
时,我会返回一个有效的光标,通过它我可以打印集合中的第一条记录。以下是我试图找到的记录:

{
    "_id" : ObjectId("539848b2119918654e7e90b1"),
    "Country" : "Antarctica",
    "Alpha2" : "AQ",
    "Aplha3" : "ATA",
    "Numeric" : "10",
    "FIPS" : "AY",
    "IGA" : ""
}

试试下面的代码,看看它是否有效

BasicDBObject query = new BasicDBObject("Country", "Antartica");

def cursor = coll.find(query)

try {
  while(cursor.hasNext()) {
    System.out.println(cursor.next());
  }
} finally {
   cursor.close();
}
有关更多信息,请查看此处:

尝试以下操作:

def cursor =  coll.find()
    def obj = cursor.next()
    while (obj.Country != 'Antarctica') {
        obj = cursor.next()
    }

这是低效的,您每次都必须遍历整个集合才能找到一条记录,但它最终会以“obj”作为您需要的记录。

您能粘贴控制器代码吗?@Lalit Agarwal post-updated-as-request.@Christian p如果我尝试使用“null”printed.cusor.next()会给出null。