Increase value with mongoose?

I have a model mongoosein my application node.jsthat represents invoices. I understood this a long time ago, but I really need to ensure that my invoices are calculated / increased in order to provide the proper link to my client.

Using an SQL database, I would create a column AUTO-INCREMENTcontaining this value, but this is not obviosly built into MongoDB. So how would this be done with mongoose?

Here's what my model looks like right now:

var InvoiceSchema = new Schema({
    reference: {type: Number, default: 0}, // The property I want to auto-incr.

    dates: {
        created:  {type: Date, default: Date.now},
        expire: {type: Date, default: expiryDate()}
    },

    amount: {type: Number, default: 0}
    // And so on
});
+5
source share
4 answers
Controller.findByIdAndUpdate(ID_HERE, {$inc: {next:1}}, function (err, data) {


});

//next - , : Number

+24

, ?

, UserSchema InvoiceSchema :

var UserSchema = new Schema({
    email: String,
    // other fields
    invoices: [{ type: Schema.Objectid, ref: 'Invoice' }]
});

var InvoiceSchema = new Schema({
    reference: { type: Schema.Objectid, ref: 'User' },

    dates: {
        created:  {type: Date, default: Date.now},
        expire: {type: Date, default: expiryDate()},
    },

    amount: {type: Number, default: 0}
    // And so on
});
+1

:

, , , , lean() exec(), , Javascript:

Controller.findByIdAndUpdate(ID_HERE, {$inc: {next:1}}, { $new: true})
   .lean().exec(function (err, data) {


});
0

All Articles