简体   繁体   English

使用Scala Salat / Casbah和Mongodb插入新记录

[英]Insert new record using Scala Salat/Casbah and Mongodb

Greetings, 问候,

I am using Salat and Casbah to create a user collection in Mongodb , everything works great until I added a unique index on the email field. 我正在使用Salat和Casbah在Mongodb创建用户集合,在我在电子邮件字段上添加唯一索引之前,一切工作都很好。 Now my insert returns a unique id with no actual record added in the DB for existing email addresses. 现在,我的插入返回唯一的ID,没有在数据库中为现有电子邮件地址添加任何实际记录。 I am new to scala and Casbah/Salat so I apologize if I am asking an obvious question. 我是scala和Casbah / Salat的新手,所以如果我问一个明显的问题,我深表歉意。

here are my collection indexes 这是我的收藏索引

db.users.getIndexes()
[
    {
    "v" : 1,
    "key" : {
    "_id" : 1
    },
    "name" : "_id_",
    "ns" : "study_test.users"
    },
    {
    "v" : 1,
    "unique" : true,
    "key" : {
        "email" : 1
    },
    "name" : "users_email",
    "ns" : "study_test.users"
    }
]

and this is my dao object 这是我的道对象

object UserDao extends SalatDAO[UserModel, ObjectId](collection = MongoUtil.getCollection("study", "users")) {
val logger = LoggerFactory.getLogger(UserDao.getClass)

val dao = this

UserDao.collection.ensureIndex(DBObject("email" -> 1), "users_email", true)
RegisterJodaTimeConversionHelpers()

def create(userContract: UserContract): Option[ObjectId] = {
    val userModel = UserConverter.toModel(userContract)
    dao.insert(userModel)

}
}

I found what the problem was, I upgraded mongodb and casbah 2.10 but used MongoConnection to connect to mongoDB. 我发现了问题所在,我升级了mongodb和casbah 2.10,但使用MongoConnection连接到mongoDB。 I switched to connection using mongo client and all works as expected. 我切换到使用mongo客户端的连接,并且一切正常。

What do you mean by "Now my insert returns a unique id with no actual record added in the DB for existing email addresses." 您的意思是“现在,我的插入内容返回一个唯一ID,而数据库中没有为现有电子邮件地址添加任何实际记录。” Can you specify an example? 你能举个例子吗?

Since you have a unique index, insert will fail for a new insert (or whatever the behaviour for scala/casbah is) for the same email id. 由于您具有唯一索引,因此对于同一电子邮件ID的新插入(或scala / casbah的行为是什么)的插入将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM