gpt4 book ai didi

mongodb - morphia save() 方法——如何判断成功或失败

转载 作者:可可西里 更新时间:2023-11-01 09:56:14 25 4
gpt4 key购买 nike

我将 Morphia 0.109 与 MongoDB 结合使用。我的文档集合使用字符串值作为 _id 字段,例如

@Entity
class MyEntity {
@Id
private String entityId;
...
}

当我调用 Datastore 或 BasicDAO save() 方法插入其中一项时,我根据需要用唯一值填充 _id 字段 (entityId)。 save() 方法返回一个 Key 类型的值。

我的问题是,如何通过检查返回的 Key 来确定保存操作是成功还是失败?我不能依靠检查 Morphia 是否填写了 _id 字段的技巧,因为我必须在调用 save() 之前填写它。

最佳答案

那是因为您使用的是 String 而不是 ObjectId。这有什么特别的原因吗?

一般来说,使用右边的WriteConcern对于您的情况,这是速度和数据安全之间的权衡。如果无法满足您规定的要求,您将遇到异常 — 例如 JOURNAL_SAFE

Exceptions are raised for network issues, and server errors; the write operation waits for the server to group commit to the journal file on disk.

关于mongodb - morphia save() 方法——如何判断成功或失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27723299/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com