conquester
conquester

Reputation: 1132

What happens to MongoDB fields which have no corresponding definition in Morphia?

I have a project which combines Python's MongoEngine and Java's Morphia.

The Python part is used for the frontend and it uses MongoEngine for document modeling. It stores a lot of information and the frontend is often changing so I will have to add or subtract new fields frequently.

The Java part does some computation task and it relies only on a few fields in the model. To map each field from MongoEngine to Java's Morphia models can be tedious since I won't be using them anyways in the Java part.

In the Morphia model, I only want to declare variables/fields which I am going to use. So what happens if I don't declare the corresponding variables for the mongo document fields in morphia? Will the original document written by the python part with much more fields be overwritten by Morphia?

Upvotes: 0

Views: 117

Answers (1)

xeraa
xeraa

Reputation: 10859

You'll need to be careful: a simple .save() will replace the document with your partial data — all the fields, which you haven't mapped in Morphia will be gone.

What you want to use is a partial update. For example the .set() operation is being translated to a $set in MongoDB — this will only change the field you have specified and this is what you want.

I'm not sure if this is really a great solution. I'd also consider either mapping the entire entity or avoiding Morphia altogether; you are only using a very small subset of the Object Document Mapper and could probably achieve the same with the plain Java client with less confusion (and it's also much better maintained).

Upvotes: 2

Related Questions