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

findOneAndReplace accepts atomic operators

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 3.2.7
    • Affects Version/s: 3.2.5
    • Component/s: None
    • Environment:
      Node v12, no mongoose

      I was surprised to see that [findOneAndReplace](https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#findOneAndReplace) did not complain when I mistakenly used it instead of `findOneAndUpdate(..., { $set:

      { ... }

      });`.

      Is it trying to silently be too friendly and perform an update when an update operator is passed instead of a replacement document? The corresponding shell command correctly errors out:

      > the replace operation document must not contain atomic operators

      Repro code:

       

      import MongoClient from 'mongodb';
      (async function main() {
       const mongoClient = await MongoClient.connect('...', { useNewUrlParser: true });
       const col = mongoClient.db().collection('bugs');
       await col.drop();
       await col.insertOne({ foo: 'bar' });
       let results = await col.find().toArray();
       console.log('Before:', results);
       const response = await col.findOneAndReplace({ foo: 'bar' }, { $set: { foo: 'quux' }});
       console.log(response);
       results = await col.find().toArray();
       console.log('After:', results);
       }());
      

       

      This doesn't throw. Here's the output with mongodb@3.2.5:

       

      Before:

      [ { _id: 5ce24b89861f562391c277ba, foo: 'bar' } ] {
         lastErrorObject: { n: 1, updatedExisting: true },
         value: { _id: 5ce24b89861f562391c277ba, foo: 'bar' },
         ok: 1,
         operationTime: Timestamp { _bsontype: 'Timestamp', low_: 4, high_: 1558334345 },
         '$clusterTime': {     clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 4, high_: 1558334345 },
           signature: { hash: [Binary], keyId: [Long] }
         }
      }
      

      After:

      [ { _id: 5ce24b89861f562391c277ba, foo: 'quux' } ]
      

            Assignee:
            katherine.walker@mongodb.com Katherine Walker (Inactive)
            Reporter:
            dandv Dan Dascalescu
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: