-->

卡斯巴光标和toList(casbah cursor and toList)

2019-10-20 01:04发布

我在卡斯巴光标,从查询返回。 如果我遍历光标我得到一定数量的结果,X。 如果我执行相同的查询,并做了光标toList,我得到大小Y,不同数量的清单。 为什么?

我是从使用默认WriteConcern刚刚wriiten几百行收集测试案例调用此。 我的理解可能会有一些延迟与写。 我不明白的是光标的大小不同:我遍历VS toList。 是不是他们基本上是做(假设我得到从我的迭代一个List)是一回事吗?

val cur = findCursor(query, orderBy).skip(skip).limit(chunkSize * -1) // results size x if I iterate cur
val ret = cur.toList.map( dbo => SJ.readDB[T](dbo) ). // List size y here after toList

Answer 1:

发现问题。 这个问题躺在传递给限制功能负值。 我不完全理解,正/负值之间的语义差别限制,为什么他们会返回不同的数,但切换到正数返回的结果计数的预期。



Answer 2:

他们应该是相同的,因为它们都以同样的方式重复下面,下面有一个例子:

import com.mongodb.casbah.Imports._
val collection = MongoClient()("test")("myColl")
collection.drop()
1 to 1000 foreach { i => collection.insert(MongoDBObject("_id" -> i)) }

val count1 = collection.count() // Get a count from the server
val count2 = collection.find().foldLeft(0)( (x, doc) => x+1) // Iterate the cursor
val count3 = collection.find().toList.length // Use toList to iterate

assert(count1 == count2)
assert(count2 == count3)

你可以得到不同的结果,如果新的文件已经数之间或者如果部分迭代光标然后将其转换列出例如添加到数据库中:

val cursor = collection.find()
cursor.next()
cursor.next()
assert(cursor.toList.length == 998)


文章来源: casbah cursor and toList