Pappa S
Pappa S

Reputation: 343

How to avoid insert empty record in mongodb using mongoose

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

Upvotes: 0

Views: 343

Answers (1)

salgado
salgado

Reputation: 48

You must define these attributes in your model.

 /* model.js */
 'use strict';

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


 function dynamicModel(suffix) {
   var addressSchema = new Schema({

    product_name: {
        type: String,
    },
    category: {
       type: Number,
    },
    title: {
       type: String,
    }

   }); 
   return mongoose.model(suffix, addressSchema); 
  } 
  module.exports = dynamicModel;

Upvotes: 1

Related Questions