Creating New Collection

suggest change

With Mongoose, a model is compiled from a schema. Let's create one. Add methods before mongoose.model(). That call compiles the schema.

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

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

AutoSchema.methods.speak = function () {
  var greeting = this.name
    ? "Hello this is  " + this.name + " and I have counts of " + this.countOf
    : "I don't have a name";
  console.log(greeting);
};

// By default the collection name is the plural of the model name.
module.exports = mongoose.model('Auto', AutoSchema);

// The optional third argument sets the collection name explicitly.
// Do not call mongoose.model() twice for the same name; the second call throws.
// module.exports = mongoose.model('Auto', AutoSchema, 'collectionName');

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents