【发布时间】:2014-06-24 16:55:24
【问题描述】:
当使用 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 不是 NULL,因为当我执行 find() 时,我确实得到了一个有效的游标,通过它我可以打印集合中的第一条记录。这是我要查找的记录:
{
"_id" : ObjectId("539848b2119918654e7e90b1"),
"Country" : "Antarctica",
"Alpha2" : "AQ",
"Aplha3" : "ATA",
"Numeric" : "10",
"FIPS" : "AY",
"IGA" : ""
}
【问题讨论】:
-
你能粘贴你的控制器代码吗?
-
@Lalit Agarwal 已按要求更新。
-
@Christian P 如果我尝试打印'null'。