question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Model.update with overwrite:true throws an error from versions 4.1.10 to 4.4.11

See original GitHub issue

It seems there has been a regression issue. Now Model.findByIdAndUpdate(id, obj, { overwrite: true }) throws an error when updating properties that are array of objects.

The document sample I’m using is the following:

var res = {
  user: "John"
  resources: [{
    foo: 1
  }, {
    bar: 2
  }]
}

Then Model.findByIdAndUpdate(doc._id, res, { overwrite: true }) throws:

"MongoError: exception: Invalid modifier specified: resources"

This is happening from versions 4.1.11 to the latest release (4.4.11).

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:8

github_iconTop GitHub Comments

3reactions
fiwscommented, Nov 24, 2016

traced this issue down to the root of the problem. overwrite breaks if you use the timestamps option for a Schema.

Example

'use strict';

var assert = require('assert');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/gh4054');
mongoose.set('debug', true);

var testSchema = new mongoose.Schema({
  user: String,
  something: Number
}, { timestamps: true }); // ← that's the problem

var TestModel = mongoose.model('gh4054', testSchema);
var options = { overwrite: true, upsert: true };
var update = {
  user: 'John',
  something: 1,
};

TestModel.update({ name: 'test' }, update, options, function(error, doc) {
  assert.ifError(error);
  console.log(doc);
});

Result

❯ node test
Mongoose: gh4054.update({ name: 'test' }, { '$set': { updatedAt: new Date("Thu, 24 Nov 2016 20:23:26 GMT") }, '$setOnInsert': { createdAt: new Date("Thu, 24 Nov 2016 20:23:26 GMT") }, user: 'John', something: 1 }, { upsert: true, overwrite: true })

events.js:160
      throw er; // Unhandled 'error' event
      ^
MongoError: Unknown modifier: user
    at Function.MongoError.create (/opt/exthome/fiws/Projects/hapi-forest/node_modules/mongodb-core/lib/error.js:31:11)
    at toError (/opt/exthome/fiws/Projects/hapi-forest/node_modules/mongodb/lib/utils.js:115:22)
    at /opt/exthome/fiws/Projects/hapi-forest/node_modules/mongodb/lib/collection.js:1046:67
    at /opt/exthome/fiws/Projects/hapi-forest/node_modules/mongodb-core/lib/connection/pool.js:455:18
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)
🔚  
0reactions
guischdicommented, Jun 18, 2017

This error still happens if the overwrite, upsert and setDefaultsOnInsert options are present at .update() options, but only if a property which has a default is not in the update object:

Example

'use strict';

var assert = require('assert');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/gh4054');
mongoose.set('debug', true);

var testSchema = new mongoose.Schema({
  name: String,
  something: { type: Number, default: 2 },
});

var TestModel = mongoose.model('gh4054', testSchema);
var options = { overwrite: true, upsert: true, setDefaultsOnInsert: true }; // ← this combination is the problem
var update1 = {
  name: 'test',
  something: 1,
};
var update2 = {
  name: 'test',
};

TestModel.update({ name: 'a' }, update1, options, function (error, raw) {
  assert.equal(raw.ok, 1);
  assert.equal(raw.nModified, 0);
  assert.equal(raw.n, 1);
  assert.equal(raw.upserted.length, 1);
  TestModel.update({ name: 'b'}, update2, options, function (error, raw) {
    assert.ifError(error);
  });
});

NOTICE: The result ist the same if you swap the second search criterion against { name: 'a' } (so no upsert but a update should be performed).

Result

❯ node test
Mongoose: gh4054.update({ name: 'a' }, { name: 'test', something: 1, __v: 0 }, { setDefaultsOnInsert: true, upsert: true, overwrite: true })
Mongoose: gh4054.update({ name: 'b' }, { '$setOnInsert': { something: 2 }, name: 'test', __v: 0 }, { setDefaultsOnInsert: true, upsert: true, overwrite: true })

events.js:182
      throw er; // Unhandled 'error' event
      ^
MongoError: Unknown modifier: name
    at Function.MongoError.create (/Users/guischdi/mongoosetest/node_modules/mongoose/node_modules/mongodb-core/lib/error.js:31:11)
    at toError (/Users/guischdi/mongoosetest/node_modules/mongoose/node_modules/mongodb/lib/utils.js:139:22)
    at /Users/guischdi/mongoosetest/node_modules/mongoose/node_modules/mongodb/lib/collection.js:1060:67
    at /Users/guischdi/mongoosetest/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:469:18
    at _combinedTickCallback (internal/process/next_tick.js:95:7)
    at process._tickCallback (internal/process/next_tick.js:161:9)
Read more comments on GitHub >

github_iconTop Results From Across the Web

[WT-8395] Inconsistent data after upgrade from 4.4.3 and 4.4.4 ...
ISSUE DESCRIPTION AND IMPACT. This issue causes incorrect checkpoint metadata to sometimes be recorded by MongoDB versions 4.4.3 and 4.4.4.
Read more >
api/node_modules/mongoose/History.md - GitLab
fix(schema): update clone method to include indexes #5268 ... fix(schema): throw error if you use prototype as a schema path #4746 ...
Read more >
Alibaba Cloud
and an updated version of this document will be released through Alibaba ... 2.1.4.4.11. ... 4.1.10. OpenAPI. 4.1.10.1. Query PMML models. 4.1.10.2.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found