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

Do not modify user input on insert operations when adding object ids

    • Type: Icon: New Feature New Feature
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      Occasionally we do get questions about documents being modified during insert operations when we attach an object id.

      We could consider changing our behavior so that the driver never modifies documents that user's provide.  There are a number of approaches, one straightforward approach would be to add support for serializing generator functions in BSON, and then "adding" an _id via the generator:

      const input = Object.freeze({
        name: 'bumpy'
      });
      
      function* addObjectId(object) {
        yield ['_id', new ObjectId()];     
        yield* Object.entries(object);
      }
      
      const output = deserialize(
          serialize(
            addObjectId(input)
          )
      );
      
      console.log(output); // { _id: new ObjectId(...), name: 'bumpy' }

      Note that this now allows users to insert frozen documents into their database without error.  This could also allow for "lazy generation" of object ids (if important) and we could also use this to preserve key order, similar to our use of maps.

      This is a low-priority feature but might be nice to have.

       

            Assignee:
            Unassigned Unassigned
            Reporter:
            bailey.pearson@mongodb.com Bailey Pearson
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: