• MongoDB的复合唯一索引


    一 创建

    • JavaScript Shell
    db.room.ensureIndex({'floor':1,'num':1})
    
    • Spring Data
    @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)插入重复数据会发生什么?

    • JavaSript Shell:重复key值异常
    > [Error] index 0: 11000 - E11000 duplicate key error collection: ...
    
    • Spring:重复key值异常
    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值异常,数据存储状态是怎样的?

    • 猜测1:所有数据回滚,发生异常,所有数据不保存
    • 猜测2:插入成功的数据入库,发生异常后,后面的数据不入库。
    • 猜测3:发生异常的数据跳过,正常数据入库。
    • 猜测4:重复key值的文档用新的数据覆盖。

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

  • 相关阅读:
    用U3D寻找看电视的感觉!!
    MipMap
    什么是 A 轮融资?有 B轮 C轮么?
    Java写的斗地主游戏源码
    sqlserver sp_spaceused用法
    SQL中的全局变量和局部变量(@@/@)
    SQL2008数据库导出到SQL2000全部步骤过程
    生成Insert语句的存储过程
    物理机连接虚拟机数据库
    配置sql server 2000以允许远程访问
  • 原文地址:https://www.cnblogs.com/linzhanfly/p/9760821.html
Copyright © 2020-2023  润新知