ReactiveMongo FindAndModify 说明
ReactiveMongo FindAndModify Clarification
我的 MongoDB 数据库中有一个集合,其中包含多个键。现在我想用一个新字段更新这个集合。所以这是我到目前为止所拥有的:
def confirm(hash: String) = {
val myDb = dbConn.db(dbName)
val userCollection = myDb[BSONCollection]("user")
val selector = BSONDocument(idKey -> BSONObjectID(hash))
val modifier = BSONDocument(
"$set" -> BSONDocument("date" -> BSONString(now.toString)) // Line 1
)
val command = FindAndModify(
userCollection.name,
selector,
Update(modifier, fetchNewObject = true)
)
myDb.command(command)
.map { user => // Line 2
Right(bidList)
}.recover {
case LastError(ok,err, code, errMsg, _) =>
Left(ServiceError(errMsg.getOrElse("failure!")))
}
}
上面的实现有两个问题:
第 1 行:这会用名为日期的新字段更新现有文档吗?
第 2 行:映射 myDb.command(命令) 给了我一个选项 [BSONDocument],但令我惊讶的是我在范围内有一个隐式转换。所以我希望它 return 一个 Option[User]!
您可以查看 .findAndUpdate
and to FindAndModifyResult
,它提供了一个 .result
操作来根据可用的 BSON 阅读器获取结果。
val person: Future[Option[AType]] = collection.findAndUpdate(
BSONDocument("name" -> "James"),
BSONDocument("$set" -> BSONDocument("age" -> 17)),
fetchNewObject = true).map(_.result[AType])
我只是花了一些时间寻找 play-reactivemongo
等效项。也许这个例子对以后的人有帮助。
val collectionFut: Future[JSONCollection] = reactiveMongoApi.database.map(_.collection[JSONCollection]("sale.numberingCounter"))
def updateIdx(nsId: NumberingSeriesId, month: Option[Int], year: Option[Int], value: Int): Future[Option[NumberingCounter]] = {
val selector = Json.obj("numberingSeriesId" -> nsId, "month" -> month, "year" -> year)
val modifier = Json.obj("$set" -> Json.obj("idx" -> value))
for {
collection <- collectionFut
writeResult <- collection.findAndUpdate(selector,modifier,upsert = true)
} yield {
writeResult.value.map(_.as[NumberingCounter])
}
}
我的 MongoDB 数据库中有一个集合,其中包含多个键。现在我想用一个新字段更新这个集合。所以这是我到目前为止所拥有的:
def confirm(hash: String) = {
val myDb = dbConn.db(dbName)
val userCollection = myDb[BSONCollection]("user")
val selector = BSONDocument(idKey -> BSONObjectID(hash))
val modifier = BSONDocument(
"$set" -> BSONDocument("date" -> BSONString(now.toString)) // Line 1
)
val command = FindAndModify(
userCollection.name,
selector,
Update(modifier, fetchNewObject = true)
)
myDb.command(command)
.map { user => // Line 2
Right(bidList)
}.recover {
case LastError(ok,err, code, errMsg, _) =>
Left(ServiceError(errMsg.getOrElse("failure!")))
}
}
上面的实现有两个问题:
第 1 行:这会用名为日期的新字段更新现有文档吗?
第 2 行:映射 myDb.command(命令) 给了我一个选项 [BSONDocument],但令我惊讶的是我在范围内有一个隐式转换。所以我希望它 return 一个 Option[User]!
您可以查看 .findAndUpdate
and to FindAndModifyResult
,它提供了一个 .result
操作来根据可用的 BSON 阅读器获取结果。
val person: Future[Option[AType]] = collection.findAndUpdate(
BSONDocument("name" -> "James"),
BSONDocument("$set" -> BSONDocument("age" -> 17)),
fetchNewObject = true).map(_.result[AType])
我只是花了一些时间寻找 play-reactivemongo
等效项。也许这个例子对以后的人有帮助。
val collectionFut: Future[JSONCollection] = reactiveMongoApi.database.map(_.collection[JSONCollection]("sale.numberingCounter"))
def updateIdx(nsId: NumberingSeriesId, month: Option[Int], year: Option[Int], value: Int): Future[Option[NumberingCounter]] = {
val selector = Json.obj("numberingSeriesId" -> nsId, "month" -> month, "year" -> year)
val modifier = Json.obj("$set" -> Json.obj("idx" -> value))
for {
collection <- collectionFut
writeResult <- collection.findAndUpdate(selector,modifier,upsert = true)
} yield {
writeResult.value.map(_.as[NumberingCounter])
}
}