tano
tano

Reputation: 896

Play2-Scala-Reactivemongo Losing Mongo schema flexibility using ReactiveMongo

I'm trying to use Play2 with ReactiveMongo to build my web application. I spent few days reading related documentation and tutorials. In my opinion one of the most powerful features of MongoDB is schema flexibility, that is the possibility of storing in the same collection documents that doesn't have exactly the same structure, but may differ one from another. For example one document may have a field that another doesn't.

Play with ReactiveMongo use case classes to implement models, but case classes obviously have a fixed structure. So all the instances of the class will have the same structure.

Does it represent a loss of flexibility? Or there is a way to implement schema flexibility with ReactiveMongo?

Upvotes: 0

Views: 148

Answers (2)

Ed Staub
Ed Staub

Reputation: 15690

In addition to Andre's answer: ReactiveMongo also supports optional fields in documents, as Options in their case classes. So you can have both the convenience and type-safety of a Scala class modelling your document and a flexible document structure.

If your document structure is one where the field-names are totally dynamic (which generally is a bad idea in Mongo), then as Andre said, you may not want to use the case-class-based ReactiveMongo document modelling at all. But you can also usually use a hybrid approach, where some aspects of a document are de/serialized dynamically using name-value maps, and some use case-classes.

Upvotes: 3

alcarv
alcarv

Reputation: 899

From what I've read in the documentations from both ReactiveMongo and ReactiveMongo Play plugin. They work with BSON and JSON structures, respectively.

It's only when you use the extensions that you have the models defined as case classes to build the DAOs. So you have all the flexibility you need or all the convenience you want. It's just a matter of choosing what structure you work with.

Upvotes: 1

Related Questions