如何在 mongo 中删除分片索引的重复文档?

How to drop duplicate documents of a sharded indices in mongo?

我在一个键上分片了一个 mongo 集群 ("link");索引它但未能确保唯一性。摄取更多数据后,我的分片字段有重复文档。 (重复链接)。是否有删除重复项的命令?我假设你在尝试 运行 这个命令后不能

db.articles.ensureIndex({"link" : 1}, {unique : true, dropDups : true});

除了您已经尝试过的命令外,没有其他直接命令。理想情况下,您将删除并重新创建此集合,指定分片键是唯一的,然后重新加载数据:

db.runCommand( { shardCollection : "articles" , key : { link : 1 } , unique : true } );

文档中很好地描述了确保分片集合唯一性的各种方法:

http://docs.mongodb.org/manual/tutorial/enforce-unique-keys-for-sharded-collections/

总体而言,您可以通过 shell 中的自定义位 javascript 删除重复项,然后使用具有唯一索引的代理集合(如上文 link 中所述)以强制执行唯一性。