1

In casbah, there are two methods called .getAs and .getAsOrElse in MongoDBObject, which returns the relevant fields' values in the type which given as the type parameter.

val dbo:MongoDBObject = ...
dbo.getAs[String](param)

This must be using type casting, because we can get a Long as a String by giving it as the type parameter, which might caused to type cast exception in runtime. Is there any other typesafe way to retrieve the original type in the result?

This must be possible because the type information of the element should be there in the getAs's output.

tiran
  • 2,389
  • 1
  • 16
  • 28

3 Answers3

1

Check out this excellent presentation on Salat by it's author. What you're looking for is Salat grater which can convert to and from DBObject.

cracked_all
  • 1,331
  • 1
  • 11
  • 26
  • I agree Salat works fine for me. This works like a JSON serializer with some adaptation for MongoDB. But in reality I use it too to generate the JSON of my MongoDB to index it on ElasticSearch – Sebastien Lorber Jan 01 '13 at 20:50
1

Disclamer: I am biased as I'm the author of Subset

I built this small library "Subset" exactly for the reason to be able to work effectively with DBObject's fields (both scalar and sub-documents) in a type-safe manner. Look through Examples and see if it fits your needs.

Alexander Azarov
  • 12,971
  • 2
  • 50
  • 54
  • Looks great! Is it also capable of querying foreign IDs (e.g. in your example tweet, the User would not be a subdocument but an ObjectId) in a typesafe way? I haven't found a solution for that yet. – Marius Soutier Aug 16 '12 at 12:42
  • Marius, I don't quite get what you are trying to achieve.. emulate `DBRef`?.. can you please open the question and invite me there, I'll try to come up with a solution – Alexander Azarov Aug 16 '12 at 14:34
  • There you go: http://stackoverflow.com/questions/11990003/mapping-mongodb-documents-to-case-class-with-types-but-without-embedded-document – Marius Soutier Aug 16 '12 at 14:58
0

The problem is that mongodb can store multiple types for a single field, so, I'm not sure what you mean by making this typesafe. There's no way to enforce it on the database side, so were you hoping that there is a way to enforce it on the casbah side? You could just do get("fieldName"), and get an Object, to be safest--but that's hardly an improvement, in my opinion.

I've been happy using Salat + Casbah, and when my database record doesn't match my Salat case class, I get a runtime exception. I just know that I have to run migration scripts when I change the types in my model, or create a new model for the new types (multiple models can be stored in the same collection). At least the Salat grater/DAO methods make it less of a hassle (you don't have to specify types every time you access a variable).

Eve Freeman
  • 32,467
  • 4
  • 86
  • 101
  • I completely agree with the Salat + Casbah combo; it completely frees you from having to worry about type safety issues! – cracked_all Aug 29 '12 at 10:45