Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-4388

Investigate NODE-4387 - Unable to use {upsert: true} with _id not null

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
    • Not Needed

      NODE-4387 Description

      What problem are you facing?

      I'm trying to upsert a document using the method `findOneAndUpdate` without having an _id: null. I've tried using the $setOnInsert clause but it seems that since the _id should not be manually set the operation is failing.

      What driver and relevant dependency versions are you using?

      NodeJS client: 3.6.3

      Mongodb server: 5.0.4

      Steps to reproduce?

      const updateClause= {
        $set: omit(update, '_id'),
        // forbidden
      {{  $setOnInsert: { _id: newObjectId() }}}
      };{}const res= await this.collection.findOneAndUpdate(

      {'device.id': customObjectID}

      , }}
      updateClause, {
       upsert: true,
       returnOriginal: false

      Running that, I get this error:

      Performing an update on the path '_id' would modify the immutable field '_id'
      That said, I cannot upsert a document with an id that is not null. If I try this operation without the $setOnInsert I get the document with _id: null.

            Unassigned Unassigned
            dbeng-pm-bot PM Bot
            0 Vote for this issue
            1 Start watching this issue