In my script i can create a new collection in mongodb but default one empty record is inserting so how to avoid that.
model.js:
/* model.js */
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
function dynamicModel(suffix) {
var addressSchema = new Schema({
product_name: {
type: String
}
});
return mongoose.model(suffix, addressSchema);
}
module.exports = dynamicModel;
data.controller.js:
var NewModel = require(path.resolve('./models/model.js'))(collectionName);
NewModel.create({ category: 1, title: 'Minion' }, function(err, doc) {
});
after created new collection I am seeing like this:
_id:ObjectId("5eceb362d538901accc0fefe");
__v:0
categoryandtitleattributes are not available in yourSchema, that's why it's creating a collection with only_idand__v.strictoption is by default enabled inSchema. If you need to add extra fields that are not specified in Schema you can set thestrictoption tofalse. mongoosejs.com/docs/guide.html#strict