mongodb在重复数据的集合里建立唯一索引
2016-03-07 13:42
796 查看
mongodb集合要求某一个字段的值唯一
通过在字段建立唯一索引来实现,现在集合里存在重复的数据,创建索引报错如下:
> db.CollectedUrl.ensureIndex({uri:1},{unique:true, dropDups:true})
{
"ok" : 0,
"errmsg" : "E11000 duplicate key error collection: uxgourmet.CollectedUr
l index: uri_1 dup key: { : \"http://www.dianping.com/search/branch/3/0_13803622
/g0\" }",
"code" : 11000
}
>
dropDups选项为true时会删除集合的重复值,但现在报错了,经过查询dropDups不支持mongodb3版本了
用以下方法解决在重复数据的集合里建立唯一索引
1.首先把集合数据导出
mongoexport -d uxgourmet -c CollectedUrl -o CollectedUrl.json
2.删除集合数据
db.CollectedUrl.remove({})
3.在集合上创建唯一索引
db.CollectedUrl.ensureIndex({uri:1},{unique:true})
4.把导出数据再次导入集合中
mongoimport -d uxgourmet -c CollectedUrl --upsert D:/CollectedUrl.json
注意要加上--upsert选项
--upsert insert or update objects that already exist
通过在字段建立唯一索引来实现,现在集合里存在重复的数据,创建索引报错如下:
> db.CollectedUrl.ensureIndex({uri:1},{unique:true, dropDups:true})
{
"ok" : 0,
"errmsg" : "E11000 duplicate key error collection: uxgourmet.CollectedUr
l index: uri_1 dup key: { : \"http://www.dianping.com/search/branch/3/0_13803622
/g0\" }",
"code" : 11000
}
>
dropDups选项为true时会删除集合的重复值,但现在报错了,经过查询dropDups不支持mongodb3版本了
用以下方法解决在重复数据的集合里建立唯一索引
1.首先把集合数据导出
mongoexport -d uxgourmet -c CollectedUrl -o CollectedUrl.json
2.删除集合数据
db.CollectedUrl.remove({})
3.在集合上创建唯一索引
db.CollectedUrl.ensureIndex({uri:1},{unique:true})
4.把导出数据再次导入集合中
mongoimport -d uxgourmet -c CollectedUrl --upsert D:/CollectedUrl.json
注意要加上--upsert选项
--upsert insert or update objects that already exist
相关文章推荐
- mongodb图形化管理工具
- mongodb可视化工具
- mongodb单机配置shard分片集群
- MongoDB常用操作命令大全
- mongodb3.2配置文件yaml格式
- linux下的mongodb的备份与恢复
- linux--mongodb安装与配置
- MongoDB中对象反序列化的一个小问题
- ubuntu 12.04 Server 上安装 nodejs, MongoDB
- mongodb 的简单使用
- MongoDB为何要部署单机副本集
- MongoDB监控项--如何使用命令得到
- appfog下用客户端管理你的mongodb、mysql(类似)
- MongoDB安装
- mongodb常用命令
- mongodb3.2安装
- 使用MongoDB的MapReduce进行区域地震信息统计实验
- windows下的mongodb下载安装
- mongodb安装以及配置-win7+下载网盘
- 前端学习总结(九)MongoDB——最出色的文档型数据库