Skip to content

$inc a default non 0 value with findOneAndUpdate and upsert true #3617

Closed
@dciccale

Description

@dciccale

hi, i am having trouble starting a counter n at a specified number and then increasing by 1 using findOneAndUpdate and upsert: true.

var RefSchema = new Schema({
  _id: {type: String, default: 'refs'},
  n: {type: Number, default: 776}
});

...

var query = {_id: 'refs'};
var update = {$inc: {n: 1}};
var options = {upsert: true, 'new': true};
// this will create the doc if it doesn't exist the first time, 
// it should use the default value from the schema instead of the value passed to $inc
RefSchema.findOneAndUpdate(query, update, options, function (err, doc) {
  console.log(doc.n); // is 1 but i expect to see 776
});

what i expect is the creation of a document with the n property starting at 776 and then increasing from that value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions