Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MongoError: Updating the path '__v' would create a conflict at '__v' #7003

Closed
Polarisation opened this issue Sep 11, 2018 · 2 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Polarisation
Copy link

Polarisation commented Sep 11, 2018

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

When using findByIdAndUpdate() or findOneAndUpdate() with the upsert option, a __v key, and $unset, the following error is thrown:

MongoError: Updating the path '__v' would create a conflict at '__v'
    at ~/mongoose-test/node_modules/mongodb-core/lib/connection/pool.js:581:63

This bug appears similar to #5973 which is supposed to have been fixed. As far as I can tell, the difference here is that in this case the $unset key is being used.

If the current behavior is a bug, please provide the steps to reproduce.

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

const GITHUB_ISSUE = `gh-7003`;
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, description: String });
  const Model = mongoose.model('Model', schema);
  const doc = await Model.create({ name: 'test', description: 'we want to unset this description later' });
  await Model.findByIdAndUpdate(doc._id, {
    '$unset': { description: '' },
    __v: 0,
    name: 'update'
  }, { upsert: true });
  console.log('updated __v');
}

When running this script, an error is thrown before the console.log.

What is the expected behavior?

The record should update without at error. Remove either __v, $unset, or the upsert option from the example and it works as expected.

Please mention your node.js, mongoose and MongoDB version.

Node.js: v10.9.0
Mongoose: v5.2.14
MongoDB: v3.6.3 (with setFeatureCompatibilityVersion: 3.6)

@lineus lineus added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Sep 14, 2018
@lineus
Copy link
Collaborator

lineus commented Sep 14, 2018

@Polarisation if you change your findByIdAndUpdate update doc to:

  await Model.findByIdAndUpdate(doc._id, {
    '$unset': { description: '' },
    '$set': {
      __v: 0,
      name: 'blargh'
    }
  }, { upsert: true });

It will work as expected.

@vkarpov15 I marked this as a confirmed bug, but now I'm not so sure. If an update doc is passed in with one dollarKey, should all top level keys be dollarKeys? Marking as bug? for now.

after giving this some more thought, either way, I think this is a bug. It's obviously a bug if we should accept a mix of top level dollar/non-dollar keys, and it's a bug if not, because we should probably be returning an Error if we don't accept the combination of top level dollar/non-dollar keys.

@lineus lineus added bug? confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. bug? labels Sep 14, 2018
@vkarpov15 vkarpov15 added this to the 5.2.17 milestone Sep 19, 2018
@vkarpov15
Copy link
Collaborator

Thanks for reporting, will investigate ASAP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants