//指定用户名和密码连接到指定的MongoDB数据库
mongo 192.168.1.200:27017/admin -u user -p password
use youDbName
1.MongoDB 查询所有索引的命令:
2.MongoDB 创建索引的命令:
Unique Index:
注:
MongoDB无法创建指定的索引字段唯一索引(S)如果集合已经包含了将违反唯一性约束的数据指标。
MongoDB cannot create a unique index on the specified index field(s) if the collection already contains data that would violate the unique constraint for the index.
https://docs.mongodb.com/v3.0/tutorial/create-a-unique-index/
解决方法:
1.把集合数据导出(mongoexport)至csv文件备份。
2.清空集合数据。
3.创建 Unique Index
4.将 步骤1 备份的csv文件导入(mongoimport)集合。会自动过滤掉重复的数据。
效果: