mongo 3 在唯一索引上重复 - dropDups [英] mongo 3 duplicates on unique index - dropDups

查看:16
本文介绍了mongo 3 在唯一索引上重复 - 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 :

  1. 使用新集合:

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆