Node.js Creating New Collection


With Mongoose, everything is derived from a Schema. Lets create a schema.

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var AutoSchema = new Schema({
    name : String,
    countOf: Number,
// defining the document structure 

// by default the collection created in the db would be the first parameter we use (or the plural of it)    
module.exports = mongoose.model('Auto', AutoSchema); 

// we can over write it and define the collection name by specifying that in the third parameters. 
module.exports = mongoose.model('Auto', AutoSchema, 'collectionName'); 

// We can also define methods in the models. 
AutoSchema.methods.speak = function () {
  var greeting =
    ? "Hello this is  " + " and I have counts of "+ this.countOf
    : "I don't have a name";
mongoose.model('Auto', AutoSchema, 'collectionName'); 

Remember methods must be added to the schema before compiling it with mongoose.model() like done above ..