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

Error with overlapping updates under array and map "Updating the path [..] would create a conflict at [..]" #11764

Closed
johnpeb opened this issue May 3, 2022 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@johnpeb
Copy link
Contributor

johnpeb commented May 3, 2022

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

What is the current behavior?
Assigning fields of an object under an array as well as assigning the object itself fails to save. See script below

 % npx ts-node scripts/mongoose
MongoServerError: Updating the path 'list.0.quantity' would create a conflict at 'list.0.quantity'
    at [..]/node_modules/mongodb/src/operations/update.ts:174:44
    at [..]/node_modules/mongodb/src/cmap/connection_pool.ts:482:13
    at handleOperationResult ([..]/node_modules/mongodb/src/sdam/server.ts:538:14)
    at Connection.onMessage ([..]/node_modules/mongodb/src/cmap/connection.ts:452:5)
    at MessageStream.<anonymous> ([..]/node_modules/mongodb/src/cmap/connection.ts:267:56)
    at MessageStream.emit (node:events:390:28)
    at MessageStream.emit (node:domain:475:12)
    at processIncomingData ([..]/node_modules/mongodb/src/cmap/message_stream.ts:168:12)
    at MessageStream._write ([..]/node_modules/mongodb/src/cmap/message_stream.ts:65:5)
    at writeOrBuffer (node:internal/streams/writable:389:12) {
  index: 0,
  code: 40,
  [Symbol(errorLabels)]: Set(0) {}
}

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

import { Schema } from 'mongoose';
import * as mongoose from 'mongoose'

run().catch(err => console.log(err));

async function run(): Promise<void> {
  await mongoose.connect('mongodb://localhost:27017/test-mongoose', {});
  await mongoose.connection.dropDatabase();

  const Test = mongoose.model('Test', new Schema({
    list: [{
      quantity: {
        value: Number,
        unit: String
      }
    }]
  }));

  await Test.create({list: [{quantity: {value: 1, unit: 'case'}}]});

  let doc = await Test.findOne();
  doc.list[0].quantity.value = null
  doc.list[0].quantity.unit = null
  doc.list[0].quantity = null // < If this is omitted there is no problem
  await doc.save();

  process.exit()
}

What is the expected behavior?
Save() does not throw error

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

mongoose 6.3.2
mongo v4.2.8
node v16.13.1

@johnpeb
Copy link
Contributor Author

johnpeb commented May 3, 2022

The same issue occurs under a map too. Let me know if I should create a separate issue.

import { Schema } from 'mongoose';
import * as mongoose from 'mongoose'

run().catch(err => console.log(err));

async function run(): Promise<void> {
  await mongoose.connect('mongodb://localhost:27017/test-mongoose', {});
  await mongoose.connection.dropDatabase();
  mongoose.set('debug', true)

  const Test = mongoose.model('Test', new Schema({
    map: {
      type: Map,
      of: {
        _id: false,
        quantity: {
          value: Number,
          unit: String
        },
      }
    }
  }));

  await Test.create({map: {key: {quantity: {value: 1, unit: 'case'}}}});

  let doc = await Test.findOne();
  doc.get('map.key').quantity.value = null
  doc.get('map.key').quantity.unit = null
  doc.get('map.key').quantity = null // < If this is omitted there is no problem
  await doc.save();

  process.exit()
}

@IslandRhythms IslandRhythms changed the title Error with overlapping updates under array "Updating the path [..] would create a conflict at [..]" Error with overlapping updates under array and map "Updating the path [..] would create a conflict at [..]" May 4, 2022
@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label May 4, 2022
@vkarpov15 vkarpov15 added this to the 6.3.4 milestone May 11, 2022
vkarpov15 added a commit that referenced this issue May 15, 2022
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