thkala
thkala

Reputation: 86333

Dealing with schema changes in MongoDB

I use MongoDB in one of my Java projects. After a DB schema change, I found myself modifying existing code at a lot of places to perform the change from e.g.:

Object result = collection.findOne();

to

Object result = collection.findOne().get("ThisField").get("ThatField");

Now, things are relatively simple in the findOne() case, but they get more complex when find() and the associated cursors come to play.

In most cases, it would have been far easier if I could modify the query, rather than its result. I have already experimented with retrieving specfic fields only, but as far as I can tell, that only masks the rest of the fields - it does not change the structure of the object.

Upvotes: 3

Views: 2639

Answers (2)

thkala
thkala

Reputation: 86333

It seems that MongoDB does not currently have any server-side equivalent of the relational database views. The MongoDB Map/Reduce support is apparently suitable only for batch operations, which makes it useless for an online database with real-time updates.

As a workaround, I turned to a client-side solution. I took advantage of the fact that the Java driver is open source and thus I could easily work out how it is structured.

More specifically, I was able to extend and replace the default BSON decoder for the collections of interest and transparently relocate the fields that had moved.

Upvotes: 2

agarcian
agarcian

Reputation: 3965

The answer to your second question is probably a Map/Reduce function. However, there are some considerations:

  • Map/Reduce is usually to do aggregate calculations, but can be used as a View in the sense that it will go through every object in the collection and will generate an output collection where your documents are aggregated (counts, sums, etc.) or are transformed (for each document, if ThisField is present return ThisField, otherwise return ThatField)

  • It runs on the server so you can schedule jobs to generate your collections (representing the concept of a View); however, you need to consider that MapReduce is not good for online queries. It is intended to do background jobs to transform data very efficiently, but not to run queries (unless you only have a small collection).

So I would add that if you want to 'migrate' your schema, you could run a map/reduce and save it as a new permanent collection with the new schema. All you need to do to continue programming against the new schema is to use the new collection.

Upvotes: 0

Related Questions