mongodb - node.js + mongoose connection and creation issue -


i want know if when set mongoose connection , define models, (previously adding appropriate requires on app.js, or wathever), model, if not exist, created automatically first time when run node app.js?

is kind of logic correct?

if not, have create before mongodb collections, models , on?

i thinking automatic creation of mongo db collection when first run app.js

thanks! michele prandina

schemas (and models) client-side (node.js) manifestation of data model. few things, indexes you've defined, created upon first use (like saving document example). else delay created, including collections.

if want consistent behavior regarding models (and associated schemas), you'll need make sure they're loaded prior access of associated database. doesn't matter put them, long created/executed prior usage. might example:

app.js  models\cheese.js       \cracker.js 

then, in app.js:

var cheese = require('cheese.js'); var cracker = require('cracker.js'); 

assuming, of course, you've exported models:

model.exports = mongoose.model('cheese',                         new mongoose.schema({                            name: string,                            color: string                         })                 ); 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -