Mongoose does not create a new collection

I have the following in server.js:

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

and a model that works great!

  var userSchema = new Schema({ firstName: { type: String, trim: true, required: true }, lastName: {type: String, trim: true, required: true}, cellPhoneNumber : {type: Number, unique: true}, email: { type: String, unique: true, lowercase: true, trim: true }, password: String }); 

and there is another model similar below that doesn't work!

 var jobSchema = new Schema({ category: {type: Number, required: true}, title: {type: String, required: true}, tags: [String], longDesc: String, startedDate: Date, views: Number, report: Boolean, reportCounter: Number, status: String, poster: String, lastModifiedInDate: Date, verified: Boolean }); 

The two var are as follows:

 var User = mongoose.model('User', userSchema); var Job = mongoose.model('Job', jobSchema); 

- mongod does not register any errors after connecting to server.js server. Does anyone know what happened to my second model?

+5
source share
2 answers

Mongoose will not create a jobs collection for the model until the first document of that model is saved.

 Job.create({category: 1, title: 'Minion"}, function(err, doc) { // At this point the jobs collection is created. }); 
+11
source

The reason is that mongoose only auto creates collections at startup that have indexes in them. Your user collection has a unique index, there are no works in the collection. Today I had the same problem.

 // example code to test var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); mongoose.model('Test', { author: { type: String, index: true } }); 
+13
source

All Articles