Skip to content

Updating nested object with timestamps causes ConflictingUpdateOperators error #9105

Closed
@tHBp

Description

@tHBp

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

What is the current behavior? If the timestamps property is targeting a nested object in the schema, any updates to the aforementioned object fail with ConflictingUpdateOperators error.

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

What is the expected behavior? It should be able to update the object

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

Software Version
NodeJS 12.18.0
mongoose 5.9.18
mongodb 4.2.6
Minimal code to reproduce bug
const mongoose = require("mongoose");

const SampleSchema = new mongoose.Schema(
  {
    someComplexObject: {
      someProperty: String,
    },
  },
  {
    timestamps: {
      updatedAt: "someComplexObject.timestamp",
    },
  }
);

const model = mongoose.model("SampleModel", SampleSchema);

(async () => {
  // Connect to database
  await mongoose.connect("mongodb://localhost:27017/dummy", {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
  });

  // Insert a dummy value to the db
  const createResult = await model.create({
    someComplexObject: {
      someProperty: "someValue",
    },
  });

  try {
   // Try to update the same nested object
    const updateResult = await model.findByIdAndUpdate(createResult.id, {
      someComplexObject: {
        someProperty: "updatedString",
      },
    });
  // Should log the result here
    console.log("Result", updateResult);
  } catch (error) {
  // Instead throws the error here
    console.error(error);
  } finally {
    await mongoose.disconnect();
  }
})();
Error
MongoError: Updating the path 'someComplexObject' would create a conflict at 'someComplexObject'
    at MessageStream.messageHandler (/mongoose-issue/node_modules/mongodb/lib/cmap/connection.js:261:20)
    at MessageStream.emit (events.js:315:20)
    at processIncomingData (/mongoose-issue/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/mongoose-issue/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at doWrite (_stream_writable.js:403:12)
    at writeOrBuffer (_stream_writable.js:387:5)
    at MessageStream.Writable.write (_stream_writable.js:318:11)
    at Socket.ondata (_stream_readable.js:717:22)
    at Socket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:295:12)
    at readableAddChunk (_stream_readable.js:271:9)
    at Socket.Readable.push (_stream_readable.js:212:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
  ok: 0,
  code: 40,
  codeName: 'ConflictingUpdateOperators'
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions