-
Type: Bug
-
Resolution: Fixed
-
Priority: Critical - P2
-
Affects Version/s: 3.2.5
-
Component/s: None
-
Environment:Node v12, no mongoose
-
Empty show more show less
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' } ]