获取reactmongo findAndUpdate函数的返回值 [英] Get the return value of reactivemongo findAndUpdate function
本文介绍了获取reactmongo findAndUpdate函数的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在mongo集合上调用findAndUpdate函数来增加一个计数器,我想获取该计数器的值以供进一步使用.这是我的代码:
I call the findAndUpdate function on a mongo collection to increase a counter, and I want to get the value of the counter for further use.Here is my code:
collection.findAndUpdate(
BSONDocument("name" -> "counter"),
BSONDocument("$inc" -> BSONDocument("count" -> 1)),
fetchNewObject = true,
upsert = true
).map{ e =>
println("count = " + e.value.getAs[Int]("count"))
//I want to do something with the count here
}
这不能编译,因为e.value.getAs[Int]("count")
似乎是错误的.
编译错误是:
This doesn't compile because e.value.getAs[Int]("count")
seems to be wrong.
The compile error is:
value getAs is not a member of Option[service.this.collection.BatchCommands.FindAndModifyCommand.pack.Document]
请告知,谢谢!
推荐答案
尝试一下:
case class Person(name: String, count: Int)
object Helpers {
def increaseCount(collection: BSONCollection): Future[Option[Int]] = {
import collection.BatchCommands.FindAndModifyCommand.FindAndModifyResult
implicit val reader = Macros.reader[Person]
val resultFut: Future[FindAndModifyResult] = collection.findAndUpdate(
BSONDocument("name" -> "James"),
BSONDocument("$inc" -> BSONDocument("count" -> 1)),
fetchNewObject = true,
upsert = true
)
val updatedCountOpt = resultFut.map { r =>
r.result[Person].map { p =>
p.count
}
}
updatedCountOpt
}
}
这篇关于获取reactmongo findAndUpdate函数的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文