我在我的MongoDB数据库中有一个集合,里面有几个键。现在我想用一个新字段更新这个集合。所以这是我到目前为止有:ReactiveMongo FindAndModify澄清
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号线:这会更新一个名为date新领域存在的文件呢?
在线2:映射myDb.command(命令)给了我一个选项[BSONDocument],但我很惊讶的是我有一个隐式转换范围。所以我会期待它返回一个选项[用户]!
您不指明您使用的是哪个版本。在RM 0.11.7中,您有收集操作'.findAndUpdate'。 – cchantep
这是针对MongoDB的ReactiveMongo 0.11.7 3.0.6 – sparkr
你可以看看[.findAndUpdate](http://reactivemongo.org/releases/0.11/api/index.html#reactivemongo.api.collections.GenericCollection findAndUpdate [Q,U]%28selector:Q,更新:U,fetchNewObject:布尔,UPSERT:布尔,排序:选项[GenericCollection.this.pack.Document],字段:选项[GenericCollection.this.pack.Document]%29 %28implicitselectorWriter:GenericCollection.this.pack.Writer [Q],implicitupdateWriter:GenericCollection.this.pack.Writer [U],implicitec:scala.concurrent.ExecutionContext%29:scala.concurrent.Future [GenericCollection.this.BatchCommands.FindAndModifyCommand .FindAndModifyResult]) – cchantep