John Smith Optional
John Smith Optional

Reputation: 24945

How to get error message from the scala/java MongoDB api

I'm using Casbah (mongodb scala library). I have an insert that doesn't work.

val builder = MongoDBObject.newBuilder
builder += "_id" -> token.uuid
builder += "email" -> token.email
builder += "creationTime" -> token.creationTime
builder += "expirationTime" -> token.expirationTime
builder += "isSignUp" -> token.isSignUp
val writeResult = mycollection += (builder.result)

If I change this for something simpler (like, a simple {"hello": "world"} document), the insert is done. So I know there's something that doesn't work with this particular insert. However, I find no way to know why. I'd like to get some feedback from Mongo or from Casbah.

However the WriteResult class, which apparently comes directly from the Java MongoDB driver, seems very opaque: http://api.mongodb.com/java/3.0/com/mongodb/WriteResult.html

How can I get some info about why an insert is failing? I'm not asking about this particular insert. Just, in general, how can I get info about the error that caused an insert to fail?

Thanks for your help.

Upvotes: 0

Views: 130

Answers (1)

Boaz
Boaz

Reputation: 1470

Casbah is a Scala wrapper over the Java MongoDB driver.

mycollection += (builder.result)

is translated into

mycollection.save(builder.result)

If the operation had an error it will throw an exception like described here.
The WriteResult containing information about the write if no error happened.

I would check:

  1. getN and isUpdateOfExisting values in WriteResult because save is doing either update or insert (read more here).

  2. wasAcknowledged value in WriteResult to make sure you get the exception and you don't have the WriteConcern set to UNACKNOWLEDGED.

Upvotes: 0

Related Questions