我已连接到mongodb并过滤数据,如下所示:
val coll = mongoDB.getTable(db, "report")
val query = new BasicDBObject()
query.put("version",getVersion)
val fields = new BasicDBObject()
fields.put("version",1)
fields.put("project",1)
fields.put("uri",1)
fields.put("test",1)
fields.put("browser",1)
val cursor = coll.find(queryObject,fields)
然后得到这样的文件:
{
"_id" : ObjectId("5a968c6cffac6135b09d6cd5"),
"project" : "com.sink.www.helper",
"uri" : "com.sink.www.helper.somketest",
"browser" : "firefox",
"develop" : "scala",
"duration" : "0.61",
"test" : "acceptance"
"version" : "1.0"
"try" : 1
}
{
"_id" : ObjectId("5a968daaffacc7195c2f9b6i"),
"project" : "com.sink.www.helper",
"uri" : "com.sink.www.helper.somketest",
"browser" : "firefox",
"develop" : "scala",
"duration" : "0.62",
"test" : "acceptance"
"version" : "1.0"
"try" : 1
}
{
"_id" : ObjectId("5a968ea5fface1a723ffr246"),
"project" : "com.sink.www.helper",
"uri" : "com.sink.www.helper.somketest",
"browser" : "chrome",
"develop" : "scala",
"duration" : "0.58",
"test" : "acceptance"
"version" : "1.0"
"try" : 1
}
.....
我的问题是当“版本”是特定值,如“1.0”,然后字段try = try + 1,我已经编写了这样的代码,但是每次都会读取mongodb,这将花费很多时间:
while (cursor.hasNext())
{
val details = cursor.next()
if (details.get("version").toString() == getVersion && details.get("browser").toString() == getBrowser)
{
try += 1
}
}
因为mongodb中有很多数据,所以它不适应我们的项目。所以我想得到特定数据到数组或其他人并得到判断然后让try + 1,我写这样的代码,但报告错误:
val scenarioArray = coll.find(queryObject,fields).toArray
for (i <- 0 to scenarioArray.length - 1)
{
println(scenarioArray(i))
}
错误:值长度不是java.util.List的成员[com.mongodb.DBObject] 错误:java.util.List [com.mongodb.DBObject]不接受参数 我不知道如何将文档检索到数组或列表或其他容器,任何人都可以得到帮助吗?感谢。