Creating New Collection
suggest changeWith 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');
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents