1

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. Now my insert returns a unique id with no actual record added in the DB for existing email addresses. I am new to scala and Casbah/Salat so I apologize if I am asking an obvious question.

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)

}
}
Himanshu
  • 4,327
  • 16
  • 31
  • 39
user1521903
  • 151
  • 2
  • 6

2 Answers2

1

I found what the problem was, I upgraded mongodb and casbah 2.10 but used MongoConnection to connect to mongoDB. I switched to connection using mongo client and all works as expected.

user1521903
  • 151
  • 2
  • 6
0

What do you mean by "Now my insert returns a unique id with no actual record added in the DB for existing email addresses." 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.

aks
  • 720
  • 4
  • 9
  • in my specs2 test I have class User extends Specification{ sequential "Insert test" should { "create new User collection" in { val user = UserContract("John", "Doe", "hohndoe@domain.com", "08/14/1982", "test", "male", 123, true) val id = UserDao.create(user) id must not equalTo None } } } if I run the test twice the second time "id" is actually Some(5362a9f6036457afc7ad1bd7) where I only see 1 record in mongo (the record from the first time I ran the test). What I am trying to say is that I expect an exception on the second run but get Some instead. – user1521903 May 01 '14 at 20:07