0

I am trying to create auto increment fields in multiple models/schemas/nested documents, so something like (marked auto increment fields with an *

 Vegetables:
    [
         {
             name: Carrot
             *number: 1
             plantings : [{*number: 1 }, {*number: 2}]
         }
         {
             name: Squash
             *number: 2
             plantings: [{*number: 1},{*number: 2}, {*number: 3}]
         }
    ]

I tried using the solution found here... Mongoose auto increment. My code is a bit different because I am using separate files for each schema/model...

/* Counter.js (I put this here because I want to re-use it this may not make sense) */
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
module.exports = mongoose.model('Counter', new Schema({
    _id: {type: String, required: true},
    seq: {type: Number, default: 0}
}));    


/* Vegetable.js */
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var PlantingSchema = require('./planting').schema;
var Counter = require('./counter');

var vegetableSchema = new Schema({
   name: String,
   number: Number
   plantings: [PlantingSchema]
});

vegetableSchema.pre('save', function(next) {
var doc = this;
Counter.findByIdAndUpdate({_id: 'entityId'}, {$inc: { seq: 1} },
   function(error, counter)   {
    if(error)
        return next(error);
    doc.number = counter.seq;
    next();
});

module.exports = mongoose.model('Vegetable', vegetableSchema);

When I attempt to run this code the 'counter' inside of the findByIdAndUpdate is a 'null' value.

I think I'm lacking some fundamental understanding of how these models/schemas work for what I'm trying to do.

I also took a look at the auto increment plugin, but all of the samples I see for this seem to want the connection to appear with the model/schema definition which doesn't really work for me using separate files for these... but if you think this is a better approach and can explain how to make the initialization work in my scenario that would be fine too. Currently my mongo connection is only being created in my server file where these models/schemas are required.

/* Note here in the whatevermymodel.js 
      does not contain an instance of the 
      mongoose/db connection */
autoIncrement.initialize(mongoose.connection);
CounterSchema.plugin(autoIncrement.plugin, 'Counter');
var Counter = mongoose.model('Counter', CounterSchema);

UPDATE

I was able to get the auto increment plugin to work by doing the

mongoose.createConnection(config.database);

in each model but I'm not sure this is a 'best practice'

Community
  • 1
  • 1
maehue
  • 505
  • 10
  • 26
  • You don't need to initialize autoIncrement in every file. – chrisbajorin Feb 26 '16 at 17:11
  • Is it only initialized in the server file? (where the api/routes are) – maehue Feb 26 '16 at 18:37
  • Yeah, you initialized it wherever you initialized the db connection. `var db = mongoose.connect("localhost/my-db"); autoIncrement.initialized(db.connection);` and you're all set. – chrisbajorin Feb 26 '16 at 19:17
  • If I move the initialization code to the server file (the two lines you referenced) within my model/schema files I am getting an error 'throw new Error("mongoose-auto-increment has not be initialized"); – maehue Feb 26 '16 at 19:32
  • are you using `createConnection()` or `connect()` when you start mongoose up? – chrisbajorin Feb 26 '16 at 19:36

0 Answers0