【问题标题】:Why is Casbah/MongoDB id index Not Unique?为什么 Casbah/MongoDB id 索引不是唯一的?
【发布时间】:2015-09-07 10:32:39
【问题描述】:

我很惊讶_id 不是唯一索引。我正在为 _id 字段提供值,MongoDB 正在创建索引,但它不是唯一的。我试图更新它(通过在_id 字段上创建一个新的唯一索引)但没有任何改变。我也没有收到任何错误。为什么会发生这种情况?如何在 _id 上创建唯一索引?

MongoDB 版本(由 version() 提供)3.0.6,Casbah 版本 2.8.2,Scala 版本 2.11.7。

我的文档结构:

{_id=1, firstName=John, lastName=Doe, phoneNum=111-111-1111, active=true, email=test@gmail.com}

在日志中转储的索引(为简洁起见省略了时间戳等)。我不确定为什么每个索引都打印两次,但这是另一个问题的问题。对于记录,这就是我打印索引的方式:collection.indexInfo.foreach { index => logger.debug(s"Index: ${index.toMap}") }

Index: {v=1, key={ "_id" : 1}, name=_id_, ns=akka.users}
Index: {v=1, unique=true, key={ "phoneNum" : 1}, name=phoneNum_1, ns=akka.users}
Index: {v=1, unique=true, key={ "email" : 1}, name=email_1, ns=akka.users, sparse=true}
Index: {v=1, key={ "_id" : 1}, name=_id_, ns=akka.users}
Index: {v=1, unique=true, key={ "phoneNum" : 1}, name=phoneNum_1, ns=akka.users}
Index: {v=1, unique=true, key={ "email" : 1}, name=email_1, ns=akka.users, sparse=true}

【问题讨论】:

    标签: mongodb scala indexing casbah unique-index


    【解决方案1】:

    在 mongo 课程视频中,声明“_id”索引未使用命令 db.collection.getIndexes() 标记为唯一,即使它是唯一的。 我在官方文档中找不到此信息。

    如果您想确定,请尝试使用现有的 _id 字段添加另一个文档。

    我的_id 索引未标记为唯一:

    > db.products.getIndexes()
    [
            {
                    "v" : 1,
                    "key" : {
                            "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "test.products"
            }
    ]
    

    添加现有索引并获取重复键错误:

    > db.products.insert({ "_id" : ObjectId("55ed6ccc20a18b075ba683b2")})
    WriteResult({
            "nInserted" : 0,
            "writeError" : {
                    "code" : 11000,
                    "errmsg" : "E11000 duplicate key error index: test.products.$_id_ dup key: { : ObjectId('55ed6ccc20a18b0
    75ba683b2') }"
            }
    })
    

    【讨论】:

    • 我在发布前检查过,它允许插入。我发现了问题,请参阅下面的答案。我会赞成你的回答有帮助但不是正确的。
    【解决方案2】:

    当我看到上面的@codename44 插入语句时,我脑子里突然冒出一个灯泡。事实证明,Mongo 在文档中拥有一个唯一的 _id 字段不仅足以确保唯一性,而且该字段还必须是 ObjectId 类型。我的问题是我将该字段插入为String。

    也就是说,我现在遇到的问题是所有插入都不起作用。没有错误,没有违反索引,但根本没有插入文档。请参阅下面的更新代码,该代码始终输入第二种情况(writeResult.getN 为 0):

    编辑:原来失败的插入实际上并没有失败。根据this SO 帖子,Java 驱动程序始终返回 0 表示插入的行数。所以除非出现异常,总是假设插入成功。

    override def createUser(user: User) = {
      val dbObj = userToDbObj(user)
    
      val result = Try(collection.insert(dbObj, WriteConcern.Safe))
    
      val newUser = user.copy(userId = Some(dbObj.get(USER_ID).toString))
    
      result match {
        case Success(writeResult) if (writeResult.getN == 1) => Some(newUser)
        case Success(writeResult) =>
          logger.error(s"Failed to create user: ${newUser}, write result: ${writeResult}."); None
        case Failure(ex) => logger.error("Failed to create user.", ex); None
      }
    }
    
    private def userToDbObj(user: User) = {
      val builder = MongoDBObject.newBuilder
    
      builder += USER_ID -> (user.userId match {
        case Some(userId) if (ObjectId.isValid(userId)) => logger.info("Using given user id."); new ObjectId(userId)
        case _ => logger.info("Generating new user id."); new ObjectId()
      })
    
      builder += (FIRST_NAME.toString -> user.firstName,
        LAST_NAME.toString -> user.lastName,
        PHONE_NUM.toString -> user.phoneNum)
    
      user.email match {
        case Some(email) => builder += EMAIL.toString -> user.email.get
        case _ =>
      }
    
      builder.result
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 2016-10-28
      • 2015-02-15
      • 2012-06-25
      • 2023-03-07
      • 1970-01-01
      相关资源
      最近更新 更多