【发布时间】:2017-12-14 11:03:19
【问题描述】:
我正在使用spring data mongodb从Spring boot与Mongodb进行通信。
文档结构:
{
"_id" : ObjectId("5a324f8bc23fa147699ee0fb"),
"_class" : "com.mongodb.User",
"name" : "Prakash",
"userId" : NumberLong(1000),
"organisation" : "Inbytes",
"createdOn" : ISODate("2017-12-14T10:16:43.173Z")
}
带索引:userId(唯一)
如果我插入任何具有"userId" : NumberLong(1000) 即已经存在的文档,
然后我得到这个错误:
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: db.user index: userId dup key: { : 1000 }"
}
})
我有一个用例,我必须每天更新 100 万条记录。他们可能会更改他们的字段,如名称、积分等......
实现此用例的一种方法是:
find if they exist, if exist, update or override, else insert.
但是这种方法对于大量记录有很高的延迟。
是否存在更好的方法来实现我的用例。本机 MongoDB-java 和 spring mongo-DB 存储库都对我有用。
【问题讨论】:
标签: java spring mongodb spring-boot spring-data-mongodb