Question

At first, I thought it was pretty cool that you can add something to an object without it being defined in the model. But now I can't imagine how this is used in a web application like a Ruby on Rails application. What use cases require dynamic fields and how does the UI allow a user to define these dynamic fields? Do you let the user set the key for the dynamic field (with a text box or something)? Do any of you know off the top of your head any demos are applications in general that showcase the real usefulness of dynamic fields? Also, if you plan on reading these dynamic fields, wouldn't you need read_attribute (in the case of RoR) with the name of the "dynamic" field already planned for ahead of time?

I'm asking this question because I have an assignment in school to wrap an application around PostgreSQL and a NoSQL database (I chose MongoDB). I have successfully done this, however, every attribute that I add to any objects that I have defined on the MongoDB side is already defined in the model. I want to show that I'm aware of this capability, but I can't come up with any reason to use dynamic fields and I can't find results on the search, "purpose of dynamic fields in mongodb" or "dynamic fields mongodb demo".

Thanks!

Was it helpful?

Solution

First of all "Dynamic Field" is a Mongoid concept, and Mongoid is just a ODM to map ruby objects into MongoDB documents. MongoDB doesn't have and doesn't need a concept of Dynamic Fields since it is schemaless. Although this theoretically means that every document in a collection can have a different structure, this is never a practical application for a MongoDB.

An ODM such as Mongoid provides a useful mechanism to define a schema at the application level rather than in the database itself. In this context there are two big benefits to Dynamic Fields.

  1. The ability to add sub documents that have varying structures. For example you can have an Animals collection. Each type of Animal could have different body parts. But in MongoDB I don't need a "tusks" column just because some animals have them.
  2. The ability to change the schema without touching the database. It is very common to add functionality to a database through additional "columns". Using Mongoid/MondoDB this can happen in the application code, as easily as changing an application class -- completely transparently with respect to the database.

OTHER TIPS

I think you took wrong aproach to asking/looking for answer to this question,in my opinion best answer will be from the source and here is a post from MongoDB blog, I hope it will help you out.

http://blog.mongodb.org/post/119945109/why-schemaless

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top