MongoDB的复合唯一索引

时间:2018-10-09 16:03:29   收藏:0   阅读:463

一 创建

db.room.ensureIndex({‘floor‘:1,‘num‘:1})
@Data // lombok
@Document(collection = "room")
@CompoundIndexes({ 
    // 唯一复合索引:楼层和房号不能相同,不然就是同一个房间了
    @CompoundIndex(name = "floor_num", def = "{‘floor‘ : 1, ‘num‘: 1}",unique=true) 
})
public class Room {
    @Id
    private String id;
    // 楼层
    private int floor;
    // 房号
    private int num;
    // 建造时间
    private Date createAt;
}

二 疑问

(1)日期字段能与其他字段复合为唯一索引吗?

可以,mongodb存储的是时间戳,实际上转换成数字进行复合比较的。

(2)插入重复数据会发生什么?

> [Error] index 0: 11000 - E11000 duplicate key error collection: ...
org.springframework.dao.DuplicateKeyException: Write failed with error code 11000 and error message ‘E11000 duplicate key error collection:...‘

Caused by: com.mongodb.DuplicateKeyException: Write failed with error code 11000 and error message ‘E11000 duplicate key error collection: 

(3)批量插入时,发生重复key值异常,数据存储状态是怎样的?

答案:猜测2是对的。mongodb不支持事务,所以猜测1不正常,mongodb不会回滚;跳过异常数据继续入库,什么鬼,哪有这么强大的数据库,猜测3不成立;猜测4是源于insert和save操作的对比,save遇到主键重复时,会使用新的值进行覆盖,但是复合唯一索引不支持这个操作

原文:https://www.cnblogs.com/linzhanfly/p/9760821.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!