user1177440
user1177440

Reputation:

Understanding Mongoose Schema better

I am relatively new to the MongoDb world, coming from a MS Sql / Entity framework environment.

I am excited about Mongo, because of:

MongoDb's ability to dynamically change the shape of the class/table/collection at run time.

Entity framework does not offer me that.

Why is that so important?

Because I would like to create a generic inventory app and have the product class/collection/table be dynamic for clients to add fields pertinent to their business that cannot be used by everyone, eg. Vin Number, ISBN number, etc.

Now I have come to learn about Mongoose and how it offers a schema, which to me detracts from the flexibility of MongoDb described above.

I have read in a few sections that there is such an animal as mixed-schema, but that appears to be dynamic relative to the data type and not the collection of properties for the given class/collection/table.

So this is my question:

If I am looking at developing a generic class/collection /table that affords clients to shape it to include whatever fields/properties they want that pertain to their business, dynamically, should I abandon the whole notion of mongoose?

Upvotes: 2

Views: 262

Answers (1)

user1177440
user1177440

Reputation:

I found a benefit today as to where a Schema may be warranted:

Allow me to preface though and say I still thoroughly am excited about the whole idea that Mongo allows a collection to be reshaped at run time in circumstances where I may need ti to be. As mentioned above, a perfect example would be an Inventory app where I would want each client to add respective fields that pertain to their business as opposed to other clients, such as a Car dealership needing a VIN Number field, or a Book store needing a ISBN Number field.

Mongo will allow me to create one generic table and let the client shape it according to his own wishes for his own database at run time - SWEET!

But I discovered today where a schema would be appropo:

If in another table that will not be 're-shapeable', say a user table, I can create a Schema for pre-determined fields and make them required, as such:

 var dbUserSchema = mongoose.Schema({
   title: {type:String, required:'{PATH} is required!'},
   FullName: {
       FirstName: {type: String, required: '{PATH} is required!'},
       LastName: {type: String, required: '{PATH} is required!'}
   }
 });

By having the respective first-name and last-name required from the schema, the database will not add any records for a user if they are not both included in the insert.

So, I guess one gets the best of both worlds: Tables that can be re-shaped and thru a schema, tables that can be rigid.

Upvotes: 1

Related Questions