无法从 ReactiveMongo 运行示例

问题描述 投票:0回答:1

我正在运行reactivemongo 0.18.6,并且在批量更新方面遇到问题,如文档的“更新文档”部分所示:http://reactivemongo.org/releases/0.1x/documentation/tutorial/write -documents.html

我逐字复制了提供的示例,并不断收到

的编译错误

类型不匹配; 发现:Seq[reactivemongo.api.collections.bson.BSONCollection 与 Singleton#UpdateCommand.UpdateElement] 必需:Iterable[_1.UpdateCommand.UpdateElement] 其中 val _1:reactivemongo.api.collections.bson.BSONCollection

scala reactivemongo
1个回答
0
投票

我有同样的问题,使用reactivemongo 1.1.0-RC9。我能够通过将 UpdateBuilder 移出理解来修复它......尽管我不知道为什么会产生影响。

我从reactivemongo文档中的示例代码开始:http://reactivemongo.org/releases/1.0/documentation/tutorial/write-documents.html

我将直接从文档复制/粘贴示例代码,以防将来发生变化:

import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global

import reactivemongo.api.bson.BSONDocument

import reactivemongo.api.bson.collection.BSONCollection

def update1(personColl: BSONCollection) = {
  val selector = BSONDocument("name" -> "Jack")

  val modifier = BSONDocument(
    "$set" -> BSONDocument(
      "lastName" -> "London",
      "firstName" -> "Jack"),
      "$unset" -> BSONDocument("name" -> 1))

  // Simple update: get a future update
  val futureUpdate1 = personColl.update.one(
    q = selector, u = modifier,
    upsert = false, multi = false)

  // Bulk update: multiple update
  val updateBuilder1 = personColl.update(ordered = true)
  val updates = Future.sequence(Seq(
    updateBuilder1.element(
      q = BSONDocument("firstName" -> "Jane", "lastName" -> "Doh"),
      u = BSONDocument("age" -> 18),
      upsert = true,
      multi = false),
    updateBuilder1.element(
      q = BSONDocument("firstName" -> "Bob"),
      u = BSONDocument("age" -> 19),
      upsert = false,
      multi = true)))

  val bulkUpdateRes1 = updates.flatMap { ops => updateBuilder1.many(ops) }
}

当我将其直接复制到我的项目中时,它编译成功。

然后我开始将一些代码移至 for 理解中。我从 BSONCollection 开始,也成功编译了:

private def testing(personColl: Future[BSONCollection]) = {
  for {
    collection <- personColl
  } yield {
    val updateBuilder1 = collection.update(ordered = true)
    val updates = Future.sequence(Seq(
      updateBuilder1.element(
        q = BSONDocument("firstName" -> "Jane", "lastName" -> "Doh"),
        u = BSONDocument("age" -> 18),
        upsert = true,
        multi = false),
      updateBuilder1.element(
        q = BSONDocument("firstName" -> "Bob"),
        u = BSONDocument("age" -> 19),
        upsert = false,
        multi = true)))

    val bulkUpdateRes1 = updates.flatMap { ops => updateBuilder1.many(ops) }
  }
}

然后我又做了一项更改:我将 UpdateBuilder 移至 for 理解中。当我这样做时,它无法编译:

private def testing(personColl: Future[BSONCollection]) = {
  for {
    collection <- personColl
    updateBuilder1 = collection.update(ordered = true)
  } yield {
    val updates = Future.sequence(Seq(
      updateBuilder1.element(
        q = BSONDocument("firstName" -> "Jane", "lastName" -> "Doh"),
        u = BSONDocument("age" -> 18),
        upsert = true,
        multi = false),
      updateBuilder1.element(
        q = BSONDocument("firstName" -> "Bob"),
        u = BSONDocument("age" -> 19),
        upsert = false,
        multi = true)))

    val bulkUpdateRes1 = updates.flatMap { ops => updateBuilder1.many(ops) }
  }
}

这是编译错误:

[my_project] $ compile  
[info] Compiling 1 Scala source to /home/greg/IdeaProjects/predictive_betting/target/scala-2.12/classes...  
[error] /home/greg/IdeaProjects/predictive_betting/app/db/mongo/base/ReactiveMongoClient.scala:157: type mismatch;  
[error]  found   : Seq[reactivemongo.api.bson.collection.BSONCollection#UpdateElement]  
[error]     (which expands to)  Seq[reactivemongo.api.collections.GenericCollection[reactivemongo.api.bson.collection.BSONSerializationPack.type] with reactivemongo.api.CollectionMetaCommands#UpdateElement]  
[error]  required: Iterable[_1.UpdateElement] where val _1: reactivemongo.api.bson.collection.BSONCollection  
[error]       val bulkUpdateRes1 = updates.flatMap { ops => updateBuilder1.many(ops) }  
[error]                                                                         ^  
[error] one error found  
[error] (compile:compileIncremental) Compilation failed  
[error] Total time: 0 s, completed Jan 20, 2024 10:54:49 AM  

再次,我不知道为什么这会产生影响,但也许它会对某人有所帮助。或者也许有人可以解释为什么它会产生影响。

© www.soinside.com 2019 - 2024. All rights reserved.