mongo 3在唯一索引上重复 - dropDups [英] mongo 3 duplicates on unique index - dropDups
问题描述
在 mongoDB 的文档中它说:3.0 版更改:dropDups 选项不再可用."
In the documentation for mongoDB it says: "Changed in version 3.0: The dropDups option is no longer available."
如果我真的想创建唯一索引并销毁重复条目,我可以做些什么(降级除外)?
Is there anything I can do (other than downgrading) if I actually want to create a unique index and destroy duplicate entries?
请记住,我每秒收到大约 300 次插入,所以我不能只删除所有重复项,并希望在我完成索引时不会有任何重复项.
please keep in mind the I receive about 300 inserts per second so I can't just delete all duplicates and hope none will come in by the time I'm done indexing.
推荐答案
是的 dropDupes
现在是 自 2.7.5 版起已弃用,因为无法正确预测在此过程中将删除哪个文档.
Yes dropDupes
is now deprecated since version 2.7.5 because it was not possible to predict correctly which document would be deleted in the process.
通常,您有 2 个选项:
Typically, you have 2 options :
使用新的集合:
Use a new collection :
- 创建一个新集合,
- 在这个新集合上创建唯一索引,
- 运行批处理以将旧集合中的所有文档复制到新集合中,并确保在此过程中忽略重复键错误.
在你自己的收藏中手动处理:
Deal with it in your own collection manually :
- 确保您不会在代码中插入更多重复的文档,
- 对您的收藏运行一批以删除重复项(如果它们不完全相同,请确保保留好的),
- 然后添加唯一索引.
对于您的特殊情况,我会推荐 第一个选项,但有一个技巧:
For your particular case, I would recommend the first option but with a trick :
- 创建具有唯一索引的新集合,
- 更新您的代码,以便您现在在两个表中插入文档,
- 运行批处理将旧集合中的所有文档复制到新集合(忽略重复键错误),
- 重命名新集合以匹配旧名称.
- 重新更新您的代码,以便您现在只在旧"集合中编写代码
这篇关于mongo 3在唯一索引上重复 - dropDups的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!