问题描述:

I am new to scala mongodb my code is

while(result.hasNext)

{

if(result.next().containsField("profilepic"))

bl+=result.next().get("profilepic").toString()

}

It works for next but what about curr().I Doesn't found any method

I want something like this

bl+=result.curr().get("profilepic").toString()

But in scala their is no method such as curr() any alternative?

网友答案:

Disclaimer: I'm fairly new to Scala, but have much more extensive Java experience.

I guess the problem with your code is that you call next() twice while in each iteration of the loop (so you're acting on two elements), while your intent is clearly to act on one. This code will probably work for you:

while(result.hasNext) {
    val curr = result.next
    if(curr.containsField("profilepic")) {
        bl += curr.get("profilepic").toString()
    }
}

Read the following section about Iterator semantics: http://www.scala-lang.org/docu/files/collections-api/collections_43.html

The important thing to understand is that each next called on an Iterator will yield a new element:

val result = Iterator("foo", "bar", "baz")
result.next == "foo" // true
result.next == "bar" // true

In your case you want to act on the same element several times. Store it in a variable (e.g. curr) and make sure you call next exactly once during each iteration.

相关阅读:
Top