MongoDB查询以从集合中删除重复的文档 [英] MongoDB query to remove duplicate documents from a collection

查看:291
本文介绍了MongoDB查询以从集合中删除重复的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从搜索框中获取数据,然后使用常规插入查询将MongoDB作为文档插入到MongoDB中.数据以以下格式存储在单词"cancer"的集合中,格式为唯一的"_id".

I take data from a search box and then insert into MongoDB as a document using the regular insert query. The data is stored in a collection for the word "cancer" in the following format with unique "_id".

{
  "_id": {
    "$oid": "553862fa49aa20a608ee2b7b"
  },
  "0": "c",
  "1": "a",
  "2": "n",
  "3": "c",
  "4": "e",
  "5": "r"
}

每个文档都有一个单词,其存储方式与上述相同.我有很多这样的文件.现在,我想从集合中删除重复的文档.我无法找到一种方法来做到这一点.救救我.

Each document has a single word stored in the same format as above. I have many documents as such. Now, I want to remove the duplicate documents from the collection. I am unable to figure out a way to do that. Help me.

推荐答案

在mongo shell中的简单解决方案:`

an easy solution in mongo shell: `

use your_db
db.your_collection.createIndex({'1': 1, '2': 1, '3': 1, etc until you reach maximum expected letter count}, {unique: true, dropDups: true, sparse:true, name: 'dropdups'})
db.your_collection.dropIndex('dropdups')

注释:

  • 如果您有许多文档,则希望此过程花费很长时间 时间
  • 注意,这将删除现有文档,最好先克隆您的收藏集,然后在此处尝试.
  • if you have many documents expect this procedure to take very long time
  • be careful this will remove documents in place, better clone your collection first and try it there.

这篇关于MongoDB查询以从集合中删除重复的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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