后,我该如何调用查找查询
我在代码中有两个查询
第一是update
查询
var modifier = BSONDocument("$set" -> BSONDocument("client_id" -> client_id,
"access_token" -> access_token,
"refresh_token" ->refresh_token,
"inserted_date" -> inserted_date))
var selecter = BSONDocument("$and" -> BSONArray(BSONDocument("account_id" -> account_id), BSONDocument("refresh_token" -> object.refreshToken)))
tokensCollection.update(selecter, modifier)
第二个是find
查询
var query = BSONDocument("$and" -> BSONArray(BSONDocument("account_id" -> account_id), BSONDocument("refresh_token" -> refresh_token)))
val resp = tokensCollection.find(query).one[AccessTokens]
var result = Await.result(resp, 15 seconds)
result.get
我的第二个find
查询是在第一个查询update
之前执行的。我有问题
method have exception:java.util.NoSuchElementException: None.get
成功更新1st Query
我希望您的tokensCollection.update()
调用也将返回某种Future[_]
。只有当Future
完成时,您的结果才能保证在数据库中可用。
您可以这样序列化两个:
val resp = tokensCollection.update(s, m).flatMap(_ => tokensCollection.find(query))
或使用for
理解:
for {
_ <- tokensCollection.update(s, m)
q <- tokensCollection.find(query)
} yield q
请注意,Await
不是您在生产中应该使用的东西;相反,到处返回Future
S,并在最终结果中致电map
。但是,如果您只是在玩耍,它对于调试可能很有用。
这是因为更新要比查询询问要多的时间多。您必须序列化查询。您必须等待更新查询的响应,而不是在执行查找查询之后。