Mongoose: Updating the path '__v' would create a conflict at '__v'

Created on 9 Jan 2018  路  38Comments  路  Source: Automattic/mongoose

It happens if I call findOneAndUpdate or findByIdAndUpdate. If I remove __v from document, it does not appear.

../node_modules/mongoose/lib/query.js:3119:9

MongoDB shell version v3.6.1
Mongoose v.4.13.9
Node v.8.9.4

confirmed-bug

Most helpful comment

Fix is in master and will be in 5.0.16

All 38 comments

@ypanyukov code samples and full stack trace please?

var interviewState = req.body;
.......
InterviewState.findByIdAndUpdate(interviewState._id, interviewState, {
  upsert: true,
  new: true
}).exec(function (err, interviewState) {
  if (err) {
    return callback(new Error('Error in calling InterviewState.findOneAndUpdate(): ' + err));
  }
  .........
});
2018-1-10 12:53:39 - error: User: 5a54b5e8e5edaa3f96335468; Url: /interview-states/5a54e707df8d3d1ad31a4e6b; Internal server error: Error: Error in calling InterviewState.findOneAndUpdate(): MongoError: Updating the path '__v' would create a conflict at '__v'
    at ../core/server/handlers/interview_states.js:448:31
    at ../node_modules/mongoose/lib/query.js:3119:9
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:228:7)

I think it could be a problem in Mongo on MacOS but anyway I think you can cover it in docs or in code

And what does req.body look like in your example?

same InterviewState object with small changes

And what does the interviewState object look like? Specifically, does it have __v?

versionKey? of course, by default. If ask is there __v in req.body - yes. But in our application we have about 20 collections and only here I have that problem and I don't know why. Also it does not reproduce on production server. And this is most strangest for me

I found when it appears in mongo code.

https://github.com/mongodb/mongo/blob/master/src/mongo/db/update/update_object_node.cpp#L283

I cannot create a bug there. Also I cannot reproduce it on Ubuntu, only on MacOS. Mongo installed via Homebrew

This is weird, I'm not able to reproduce your error. The following code outputs the log at the end without error:

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;

const GITHUB_ISSUE = `gh-5973`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;

run().catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect(connectionString);

 const schema = new mongoose.Schema({ name: String });

 const Model = mongoose.model('Model', schema);

 const doc = await Model.create({ name: 'test' });

 await Model.findByIdAndUpdate(doc._id, { __v: 123, name: 'update' });

 console.log('updated __v')
}

I'm running MacOS Sierra 10.12.3, latest mongoose and mongodb 3.4

Latest mongodb is 3.6.2 and I really don't why it happens only here

screenshot 2018-01-16 08 19 17

I reinstalled mongo and still have same problem. I would not like to install another version of mongo, I wanna solve this issue.

Having this same issue when using findOneAndUpdate. Doesn't happen on my colleagues machines. I have mongo running in an Ubuntu 17.04. One of my colleagues have the same Ubuntu version as me. Installed using apt. I use the same version of mongo.

@rerissondaniel can you please provide code samples?

[Stream Log] error while saving segregatedCardData MongoError: Updating the path '__v' would create a conflict at '__v'
at Function.MongoError.create (/home/pk/Syook/syook-tnt-server/node_modules/mongodb-core/lib/error.js:31:11)
at /home/pk/Syook/syook-tnt-server/node_modules/mongodb-core/lib/connection/pool.js:497:72
at authenticateStragglers (/home/pk/Syook/syook-tnt-server/node_modules/mongodb-core/lib/connection/pool.js:443:16)
at Connection.messageHandler (/home/pk/Syook/syook-tnt-server/node_modules/mongodb-core/lib/connection/pool.js:477:5)
at Socket. (/home/pk/Syook/syook-tnt-server/node_modules/mongodb-core/lib/connection/connection.js:331:22)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:595:20)
* this is my stack trace **
while doing findOneAndUpdate even for me the error came up "_v" path error and this is working fine on v3.4.7 but error on 3.6.0

async function _update(newNode) {
    return new Promise((resolve, reject) => {
        if (!newNode._id) {
            resolve();
        } else {
            Node.findOneAndUpdate({_id: newNode._id}, newNode, {
                upsert: true,
                'new': true
            }).populate('parents').then(node => {
                ParentNode.find({node_id: node.node_id}).then(parentNodes => {
                    const promises = [];
                    parentNodes.forEach(parentNode => {
                        parentNode.statesName = node.statesName;
                        promises.push(parentNode.save());
                    });

                    Promise.all(promises).then(resolve(node)).catch(error => reject(error));
                })
            });
        }
    });
}

This is where I'm executing the update and get the error.

/*jshint esversion: 6 */
/*global require, exports, module */
/*eslint no-console: 0 */
/* jshint strict: false */

let mongoose = require('mongoose');

let ParentNodeSchema = new mongoose.Schema({
    node_id: {
        type: Number,
        required: true
    },

    weight: {
        type: Number,
        required: true,
    },

    name: {
        type: String,
        required: true
    },

    statesName: {
        type: [String]
    }
});

const ParentNode = mongoose.model('ParentNode', ParentNodeSchema);
module.exports = ParentNode;

This is the ParentNode model.

/*jshint esversion: 6 */
/*global require, exports, module */
/*eslint no-console: 0 */
/* jshint strict: false */

const mongoose = require('mongoose');
const autoIncrement = require('simple-mongoose-autoincrement');

const MEDIUM_EVIDENCE_VAL = 2;

const NodeSchema = new mongoose.Schema({
        node_id: {
            type: Number
        },

        name: {
            type: String,
            default: 2
        },

        variance: {
            type: Number,
            default: 0.005
        },

        states: {
            type: [Number],
            default: [0, 0, 0, 0, 0]
        },

        statesName: {
            type: [String],
            default: ["Very Low", "Low", "Medium", "High", "Very High"]
        },

        evidence: {
            type: Number,
            default: MEDIUM_EVIDENCE_VAL
        },

        function: {
            type: String
        },

        npt: {
            type: [Number],
            default: [1, 1, 1, 1, 1]
        },

        parents: [{
            type: mongoose.Schema.Types.ObjectId,
            ref: 'ParentNode',
            autopopulate: true
        }],
    },
    {usePushEach: true}
);

NodeSchema.plugin(autoIncrement, {field: 'node_id'});

const Node = mongoose.model('Node', NodeSchema);

module.exports = Node;

This is the Node Model.
I'm using mongoose for my schemas.

After running db.adminCommand({setFeatureCompatibilityVersion: "3.4"}) in mongo
the error doesn't occur anymore.

It happened to me as well, findOneAndUpdate on an _id.

Thanks @rerissondaniel I managed to solve it as well with db.adminCommand({setFeatureCompatibilityVersion: "3.4"}).

I had also to go back to mongoose 4.13, I had "cannot pass logical session id unless fully upgraded to featureCompatibilityVersion 3.6" with mongoose 5...

I got the same thing. But note if you don't need the versionKey then you could simply do
var EventSchema = new mongoose.Schema({ name: String, info: String, active: Boolean }, { versionKey: false });

I also get this error on mongo v3.6.2 for findOneAndUpdate as well as updateOne
build environment:
distmod: 2008plus-ssl
distarch: x86_64
target_arch: x86_64

mongoose v5.0.1
node v8.9.4

Im still not able to reproduce this error. I've tried on mongodb 3.6.2 and mongodb 3.4. I'm running mac OS sierra, using latest mongoose and node 8.0.0. I've also tried on node 8.9.2.

Not sure what I'm doing wrong here, I'm still using my same script because its reproducible:

const mongoose = require('mongoose');
mongoose.Promise = global.Promise;

const GITHUB_ISSUE = `gh-5973`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;

run().catch(error => console.error(error.stack));

async function run() {
  await mongoose.connect(connectionString);

 const schema = new mongoose.Schema({ name: String });

 const Model = mongoose.model('Model', schema);

 const doc = await Model.create({ name: 'test' });

 await Model.findOneAndUpdate({ _id: doc._id }, { __v: 123, name: 'update' });

 console.log('updated __v')
}

Just upgraded mongoDB from 3.4.10 to 3.6.2 and now I have same problem on Ubuntu. But more interesting that I have it only for one collection. Which information do you need from me? Because this is exactly the bug

Exactly the same problem here. I had used the angular-fullstack generator to generate my app and I got the issue after update to last mongo version (3.6.2).
Running the command db.adminCommand({setFeatureCompatibilityVersion: "3.4"}) on mongo shell fix the problem.

Seems that option it's really necessary after the update:
https://docs.mongodb.com/manual/reference/command/setFeatureCompatibilityVersion/

This is workaround, I'm not sure that this is good. But as temporary solution should be pretty well

It works but not 100%, now I found this error also after some updates on the same document.

message: "No matching document found for id "5a75ca2b4f21e3594b242be4""
name:"VersionError"

Seems still related to that versioning issue.
versionKey: false on doc Schema to temporary fix

@skarface do you think it happened to a doc that was created in mongodb 3.4 but was updated after upgrading to 3.6?

Yes, it was in my case. But after I drop the database and create it again with mongodb 3.6 and the issue remains.
My mongoose version is [email protected]

I'm getting the same error on Windows. The same data and code work fine on MacOS running MongoDB v3.4.9, [email protected].

On Windows 10:
MongoDB: mongodb-win32-x86_64-2008plus-ssl-3.6.2
Mongoose: v4.13.11
Node: v8.9.4

I did some testing on Windows where I am experiencing the issue. Results are:

MongoDB 3.6.2, [email protected], _Error: YES_
MongoDB 3.4.9, [email protected], _Error: NO_
MongoDB 3.4.9, [email protected], _Error: NO_

In short, downgrading to MongoDB 3.4.9 fixed the issue.

Btw, my data was created in version of MongoDB below v3.6.2, most likely ~v3.4 on MacOS. I am exporting my data using npm module mongodb-backup and importing it using Model.create() method.

Update 2:
On MongoDB 3.6.2, with the same code that is using findOneAndUpdate():

  • Created a new record, _Error: NO_
  • Updated the record, _Error: YES_

This means the issue is reproducible even on data created on MongoDB 3.6.2.

Came across this error today and would like to share what I found. It happens on my machine with these versions

"mongoose": "^5.0.7"
mongodb 3.6.2
Windows 10

This happens on findOneAndUpdate only when upsert is set to true,, if not using upsert it works fine.

I just stumbled upon the same error in one of our applications and while we don't use Mongoose (we use the mongodb driver directly), I believe the cause is the same.

I'm not sure whether the change in MongoDB 3.6 was intentional or not (I cannot find it the release notes), however, it seems that MongoDB now doesn't like if you set the same field in $set and $setOnInsert.

You can reproduce it directly with MongoDB shell:

// raw.js

db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )

db.testing.insert({
  foo: 123
});

const query = {
  foo: 123
};

const update = {
  $set: {
    __v: 123
  },

  $setOnInsert: {
    __v: 0
  }
};

db.testing.findOneAndUpdate(query, update, { upsert: true });
mongo testing123 raw.js

MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017/testing123
MongoDB server version: 3.6.3
2018-03-23T18:05:24.653+0100 E QUERY    [thread1] Error: findAndModifyFailed failed: {
    "ok" : 0,
    "errmsg" : "Updating the path '__v' would create a conflict at '__v'",
    "code" : 40,
    "codeName" : "ConflictingUpdateOperators"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DBCollection.prototype.findAndModify@src/mongo/shell/collection.js:724:1
DBCollection.prototype.findOneAndUpdate@src/mongo/shell/crud_api.js:857:12
@raw.js:21:1
failed to load: raw.js

Setting the feature compatibility level to "3.4" makes it work.

I think this is exactly what Mongoose does under the hood when invoking findOneAndUpdate. Change the feature compatibility level back to "3.6" and run the following (based on @varunjayaraman's code):

// mon.js

'use strict';

const mongoose = require('mongoose');

const GITHUB_ISSUE = "gh-5973";
const connectionString = `mongodb://localhost:27017/${GITHUB_ISSUE}`;

async function run() {
  await mongoose.connect(connectionString);

  const schema = new mongoose.Schema({ name: String });
  const Model = mongoose.model('Model', schema);

  const doc = await Model.create({ name: 'test' });
  await Model.findOneAndUpdate({ _id: doc._id }, { __v: 123, name: 'update' }, { upsert: true });
}

run().catch(console.error);
node mon.js
MongoError: Updating the path '__v' would create a conflict at '__v'
    at /private/tmp/tp-20180323172637/node_modules/mongodb-core/lib/connection/pool.js:595:61
    at authenticateStragglers (/private/tmp/tp-20180323172637/node_modules/mongodb-core/lib/connection/pool.js:513:16)
    at Connection.messageHandler (/private/tmp/tp-20180323172637/node_modules/mongodb-core/lib/connection/pool.js:549:5)
    at emitMessageHandler (/private/tmp/tp-20180323172637/node_modules/mongodb-core/lib/connection/connection.js:309:10)
    at Socket.<anonymous> (/private/tmp/tp-20180323172637/node_modules/mongodb-core/lib/connection/connection.js:452:17)
    at Socket.emit (events.js:180:13)
    at addChunk (_stream_readable.js:269:12)
    at readableAddChunk (_stream_readable.js:256:11)
    at Socket.Readable.push (_stream_readable.js:213:10)
    at TCP.onread (net.js:578:20)

At the end of your schema, add the versionKey: false option, if you don't need version keys.

var schema = new mongoose.Schema({
   ...
}, {
  versionKey: false
});

Fix is in master and will be in 5.0.16

I upgraded Mongoose from 5.0.9 to 5.2.4 and I can confirm this is now fixed.

Hi, great to see that this is resolved. Our cloud provider updated our mongo, and now we experience this as well. At the moment we are not able to upgrade to version 5. Any chance this can be ported to 4 as well?

@CarlRosell what prevents you from upgrading to Mongoose 5? We'd prefer to not have to backport unless absolutely necessary.

I was having the same problem, but it was fixed after updating mongoose to version 5.2.4.

@vkarpov15 right now the only thing keeping us from updating was _time_, it's a legacy application that up until now "just worked". I manage to rewrite part of the application so this is no longer an issue 馃憤

I am still facing the same issue.
@vkarpov15 This fixed when using findOneAndUpdate but it still occurs when using update.

Example using the above script by @jiripospisil


'use strict';

const mongoose = require('mongoose');

const GITHUB_ISSUE = "gh-5973";
const connectionString = `mongodb://localhost:27017/${GITHUB_ISSUE}`;

async function run() {
  await mongoose.connect(connectionString);

  const schema = new mongoose.Schema({ name: String });
  const Model = mongoose.model('Model', schema);

  const doc = await Model.create({ name: 'test' });
  await Model.update({ _id: doc._id }, { __v: 123, name: 'update' }, { upsert: true });
}

run().catch(console.error);

@sidoshi thanks for reporting, will fix asap

I experienced this error with version 5.2.13 so not convinced this is fixed.

@Polarisation this particular issue is fixed. If you're still experiencing an issue with the same error message, please open up a new issue with detailed code samples.

Was this page helpful?
0 / 5 - 0 ratings