【问题标题】:casbah cursor and toListcasbah 游标和 toList
【发布时间】:2014-06-17 13:42:33
【问题描述】:

我在 casbah 中有一个游标,从查询返回。如果我遍历光标,我会得到一定数量的结果,x。如果我执行相同的查询并在光标上执行 toList,我会得到大小为 y 的列表,这是一个不同的数字。为什么?

我从一个测试用例中调用它,该测试用例刚刚使用默认的 WriteConcern 将几百行写入集合。我知道写入可能会有一些延迟。我不明白的是光标的不同大小:我迭代与 toList。他们基本上不是在做同样的事情吗(假设我从我的迭代中产生了一个列表)?

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

【问题讨论】:

标签: mongodb scala casbah


【解决方案1】:

问题找到了。问题在于传递给限制函数的负值。我不完全理解要限制的 pos/neg 值之间的语义差异,或者为什么它们会返回不同的计数,但切换到正数会返回预期的结果计数。

【讨论】:

    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-17
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 1970-01-01
      相关资源
      最近更新 更多