Insert with unique indexes, Salat + Casbah

104 views
Skip to first unread message

zaid emeish

unread,
Apr 28, 2014, 9:16:51 PM4/28/14
to scala...@googlegroups.com
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)

  }

and this is the unit test I am running

class User extends Specification{

  val logger = LoggerFactory.getLogger("User Test")

  sequential

  "Insert test" should {
    "create new User collection" in {
      val user = UserContract("John", "Doe", "joh...@domain.com", "08/14/1982", "test", "male")
      val id = Try(UserDao.create(user)).toOption.flatten
      logger.debug("id was " + id)
      id must not equalTo None
    }
  }
}

zaid emeish

unread,
May 5, 2014, 9:12:13 PM5/5/14
to scala...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages