Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB, Mongoose and Node.js. Why declare a model?

I am new to document oriented databases. My question is why do I have to declare a model in MongoDB each and every time my app starts? For example:

mongoose.model('User', {
  collection: 'user',
  properties: [
    'created',
    'username',
    'password',
    'email'
  ]}  //Edited: '}' was missing in original post
);

I want to build all tables ONCE and in my app only feed or query for data. I thought that all data architecture should be set via CLI or a special Admin (let's say PhpMyAdmin for mySql).

It turns out that in the beginning of my application each and every time I declare a data model. Doesn't make sense to me. Help :)

like image 358
Pono Avatar asked Apr 19 '11 09:04

Pono


1 Answers

You need to declare a model because essentially MongoDB itself only stores collections and doesn't really bind to a schema/model. Mongoose provides a method to define a model but there are drivers which really don't even provide one (see Candy for Ruby). That's the whole point of having a document-orient database. You are not really binded to a schema and makes changes in the data structure of your database on the fly, as per requirement.

like image 191
neebz Avatar answered Oct 05 '22 08:10

neebz