MongoDB去除重复数据

mongodb一个很强大的非关系数据库,用来存取key-value形式的数据。当存数据时,难免会有重复数据。通过查阅资料,这里有两种方法可以去除重复数据。

1.如果你用的是mongodb 2.x版本,那去重可以可以用如下方法:

   db.ensureIndex({‘index_name':1},{'unique':1,'dropDups':1})

2.如果你用的是mongodb 3.x版本,那就稍微麻烦点了,分为三步:
  1.创建集合
   db.createCollection(name, options)

  2.建立唯一索引
  db.ensureIndex({‘index_name':1},{'unique':1,'dropDups':1})

  3.集合拷贝
  db..find().forEach(function(d){ db.getSiblingDB('')[''].insert(d); });



你可能感兴趣的:(数据库)