问题描述
我在试图用我的play2应用reactivemongo一些嵌套调用相结合的过程。
我从 createObjects
返回的对象的列表。我然后循环就过去了,检查对象的集合中存在,如果不将其插入
I'm in the process of trying to combine some nested calls with reactivemongo in my play2 application.I get a list of objects returned from createObjects
. I then loop over them, check if the object exist in the collection and if not insert them:
def dostuff() = Action {
implicit request =>
form.bindFromRequest.fold(
errors => BadRequest(views.html.invite(errors)),
form => {
val objectsReadyForSave = createObjects(form.companyId, form.companyName, sms_pattern.findAllIn(form.phoneNumbers).toSet)
Async {
for(object <- objectsReadyForSave) {
collection.find(BSONDocument("cId" -> object.get.cId,"userId" ->
object.userId.get)).cursor.headOption.map { maybeFound =>
maybeFound.map { found =>
Logger.info("Found record, do not insert")
} getOrElse {
collection.insert(object)
}
}
}
Future(Ok(views.html.invite(form)))
}
})
}
我觉得这种方式并不好,因为它可以,感觉不是play2和reactivemongo。
所以我的问题是:我应该如何构建我的嵌套调用得到我想要的结果
并获得已插入其中的物体的信息?
I feel that this way is not as good as it can be and feels not "play2" and "reactivemongo".So my question is: How should I structure my nested calls to get the result I wantand get the information of which objects that have been inserted?
推荐答案
我不是在MongoDB中无论是在ReactiveMongo专家,但你似乎正试图以同样的方式使用的NoSQL数据库,你可以使用标准的SQL数据库。注意,MongoDB是异步这意味着操作可以在未来的某个被执行,这是为什么插入/更新操作不返回受影响的文件。关于你的问题:
I am not an expert in mongoDB neither in ReactiveMongo but it seems that you are trying to use a NoSQL database in the same way as you would use standard SQL databases. Note that mongoDB is asynchronous which means that operations may be executed in some future, this is why insertion/update operations do not return affected documents. Regarding your questions:
1要插入的对象,如果他们不存在,并获得已插入?哪些对象的信息的
您或许应该看看MongoDB的方法,并与 UPSERT
参数为真调用它。如果你能负担得起,这将不是更新的文件如果他们已经在数据库中存在或以其他方式插入。同样,这种操作不返回受影响的文件,但你可以检查通过访问的。见 reactivemongo.api.collections.GenericCollection#更新它返回一个未来[LastError]
。
You should probably look at the mongoDB db.collection.update() method and call it with the upsert
parameter as true. If you can afford it, this will either update documents if they already exist in database or insert them otherwise. Again, this operation does not return affected documents but you can check how many documents have been affected by accessing the last error. See reactivemongo.api.collections.GenericCollection#update which returns a Future[LastError]
.
的 2对于插入的所有对象,将它们添加到列表,然后按OK()调用返回。的
再一次,插入/更新的文件将不予退还。如果你真的需要完整的受影响的文档返回回来,你将需要再作查询,检索匹配文档。
Once again, inserted/updated documents will not be returned. If you really need to return the complete affected document back, you will need to make another query to retrieve matching documents.
我可能会重写你的code这样(没有错误/故障处理):
I would probably rewrite your code this way (without error/failure handling):
def dostuff() = Action {
implicit request =>
form.bindFromRequest.fold(
errors => BadRequest(views.html.invite(errors)),
form => {
val objectsReadyForSave = createObjects(form.companyId, form.companyName, sms_pattern.findAllIn(form.phoneNumbers).toSet)
Async {
val operations = for {
data <- objectsReadyForSave
} yield collection.update(BSONDocument("cId" -> data.cId.get, "userId" -> data.userId.get), data, upsert = true)
Future.sequence(operations).map {
lastErrors =>
Ok("Documents probably inserted/updated!")
}
}
}
)
}
又见斯卡拉期货:
这是非常有用的! ;)
This is really useful! ;)
这篇关于我应该如何结构,我play2应用我嵌套reactivemongo电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!