I have the following model for mongoose.model('quotes')
:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var quotesSchema = new Schema({
created: { type: String, default: moment().format() },
type: { type: Number, default: 0 },
number: { type: Number, required: true },
title: { type: String, required: true, trim: true},
background: { type: String, required: true },
points: { type: Number, default: 1 },
status: { type: Number, default: 0 },
owner: { type: String, default: "anon" }
});
var settingsSchema = new Schema({
nextQuoteNumber: { type: Number, default: 1 }
});
// Save Setting Model earlier to use it below
mongoose.model('settings', settingsSchema);
var Setting = mongoose.model('settings');
quotesSchema.pre('save', true, function(next) {
Setting.findByIdAndUpdate(currentSettingsId, { $inc: { nextQuoteNumber: 1 } }, function (err, settings) {
if (err) { console.log(err) };
this.number = settings.nextQuoteNumber - 1; // substract 1 because I need the 'current' sequence number, not the next
next();
});
});
mongoose.model('quotes', quotesSchema);
There is an additional Schema for mongoose.model('settings')
to store an incrementing number for the incrementing unique index Quote.number
im trying to establish. Before each save, quotesSchema.pre('save')
is called to read, increase and pass the nextQuoteNumber
as this.number
to the respectively next()
function.
However, this entire .pre('save')
function does not seem to trigger when saving a Quote
elsewhere. Mongoose aborts the save since number
is required but not defined and no console.log()
i write into the function ever outputs anything.
pre('save')
whenever actually saving, since it ended up not happening all that often. I take it that if i wouldn't have setrequired: true
on mynumber
, my variant would have worked too? – Meletius