5、唯一索引
先看看之前创建的,rsc索引:
index ---------
{ "name" : "rsc_1" , "ns" : "ms_basic.schedule" , "key" : { "rsc" : 1}}
在rsc字段上,创建唯一索引:
coll.dropIndex(new BasicDBObject("rsc", 1));
coll.ensureIndex(new BasicDBObject("rsc", 1).append("unique", true));
显示结果:
index ---------
{ "name" : "rsc_1_unique_" , "ns" : "ms_basic.schedule" , "key" : { "rsc" : 1 , "unique" : true}}
存在如下rsc=的数据:
{ "_id" : { "$oid" : "4c2fe764dd969674cb31a0be"} , "liveStatus" : 0 , "startDate" : "2010-11-12" , "isList" : 0 , "location" : "CAG" , "posttime" : "2010-04-01 15:04:57.0" , "rsc" : "WRM160513" , "noliveTvName" : "" , "endDate" : "2010-11-12" , "endTime" : "09:30" , "isGold" : 0 , "version" : 0 , "startTime" : "09:30" , "disciplineName" : "摔跤" , "event" : "160" , "gender" : "M" , "isMedal" : 0 , "discipline" : "WR" , "venueId" : "CAG" , "phaseName" : "资格赛" , "isRecommend" : 0 , "isData" : 0 , "eventUnitType" : "HATH" , "chatroomStatus" : 0 , "isChina" : 0 , "isStartTime" : 0 , "competitionStatus" : 0 , "unit" : "13" , "tvName" : "男子古典式 - 60公斤级资格赛" , "flag" : 0 , "scheduleName" : "资格赛" , "isEndTime" : 0 , "rid" : "" , "eventName" : "男子古典式 - 60公斤级" , "phase" : "5" , "isResult" : 0}
往表data中插入一条记录。
System.out.println("-----------华丽分隔线---------------");   
    Schedule schedule = new Schedule();
    schedule.setRsc("WRM160513");
    schedule.setScheduleName("重复值");
    BasicDBObject val = new BasicDBObject();
    val.put("rsc","WRM160513");
    val.put("scheuleName","kaokao");
coll.insert(val);
没有报错,查询结果:
{ "_id" : { "$oid" : "4c2fe764dd969674cb31a0be"} , "liveStatus" : 0 , "startDate" : "2010-11-12" , "isList" : 0 , "location" : "CAG" , "posttime" : "2010-04-01 15:04:57.0" , "rsc" : "WRM160513" , "noliveTvName" : "" , "endDate" : "2010-11-12" , "endTime" : "09:30" , "isGold" : 0 , "version" : 0 , "startTime" : "09:30" , "disciplineName" : "摔跤" , "event" : "160" , "gender" : "M" , "isMedal" : 0 , "discipline" : "WR" , "venueId" : "CAG" , "phaseName" : "资格赛" , "isRecommend" : 0 , "isData" : 0 , "eventUnitType" : "HATH" , "chatroomStatus" : 0 , "isChina" : 0 , "isStartTime" : 0 , "competitionStatus" : 0 , "unit" : "13" , "tvName" : "男子古典式 - 60公斤级资格赛" , "flag" : 0 , "scheduleName" : "资格赛" , "isEndTime" : 0 , "rid" : "" , "eventName" : "男子古典式 - 60公斤级" , "phase" : "5" , "isResult" : 0}
注意:java应用中,有个问题,如果数据已经重复了,那么使用:
coll.ensureIndex(new BasicDBObject("rsc", 1).append("unique", true));
是没有错误提示的,此时继续执行插入操作,是成功的。
{ "_id" : { "$oid" : "4c2fe764dd969674cb31a0be"} , "liveStatus" : 0 , "startDate" : "2010-11-12" , "isList" : 0 , "location" : "CAG" , "posttime" : "2010-04-01 15:04:57.0" , "rsc" : "WRM160513" , "noliveTvName" : "" , "endDate" : "2010-11-12" , "endTime" : "09:30" , "isGold" : 0 , "version" : 0 , "startTime" : "09:30" , "disciplineName" : "摔跤" , "event" : "160" , "gender" : "M" , "isMedal" : 0 , "discipline" : "WR" , "venueId" : "CAG" , "phaseName" : "资格赛" , "isRecommend" : 0 , "isData" : 0 , "eventUnitType" : "HATH" , "chatroomStatus" : 0 , "isChina" : 0 , "isStartTime" : 0 , "competitionStatus" : 0 , "unit" : "13" , "tvName" : "男子古典式 - 60公斤级资格赛" , "flag" : 0 , "scheduleName" : "资格赛" , "isEndTime" : 0 , "rid" : "" , "eventName" : "男子古典式 - 60公斤级" , "phase" : "5" , "isResult" : 0}
{ "_id" : { "$oid" : "4c3334ea76c7eebe35fb1f81"} , "rsc" : "WRM160513" , "scheduleName" : "kaokao"}
java中,生成索引结果:
> db.schedule.getIndexes();
[
        {
                "name" : "_id_",
                "ns" : "ms_basic.schedule",
                "key" : {
                        "_id" : 1
                }
        },
        {
                "name" : "rsc_1_unique_",
                "ns" : "ms_basic.schedule",
                "key" : {
                        "rsc" : 1,
                        "unique" : true
                }
        }
]
命令行结果:
> db.schedule.ensureIndex({rsc:1},{unique: true});
> db.schedule.getIndexes();
[
        {
                "name" : "_id_",
                "ns" : "ms_basic.schedule",
                "key" : {
                        "_id" : 1
                }
        },
        {
                "_id" : ObjectId("4c3335de22090000000065d5"),
                "ns" : "ms_basic.schedule",
                "key" : {
                        "rsc" : 1
                },
                "name" : "rsc_1",
                "unique" : true
        }
]
后来检查发现:
应该使用:coll.ensureIndex(new BasicDBObject("rsc", 1),"rsc",true);
而不是:coll.ensureIndex(new BasicDBObject("rsc", 1).append("unique", true));
这种方式相当于创建了组合索引:rsc_unique,是错误的
认真看看API文档说明即可发现。