Skip to content

Commit 2af5994

Browse files
committed
NODE-899 Make upsertedId value from updateOne compatible with pre 2.2.12
1 parent d913634 commit 2af5994

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

lib/collection.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,12 @@ var updateOne = function(self, filter, update, options, callback) {
896896
r.upsertedId = Array.isArray(r.result.upserted) && r.result.upserted.length > 0 ? r.result.upserted[0]._id : null;
897897
r.upsertedCount = Array.isArray(r.result.upserted) && r.result.upserted.length ? r.result.upserted.length : 0;
898898
r.matchedCount = Array.isArray(r.result.upserted) && r.result.upserted.length > 0 ? 0 : r.result.n;
899+
900+
// Fix for NODE-899, backward compatibility with legacy format
901+
if(r.upsertedId) {
902+
r.upsertedId._id = r.upsertedId;
903+
}
904+
899905
if(callback) callback(null, r);
900906
});
901907
}

test/functional/crud_api_tests.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,8 @@ exports['should correctly execute update methods using crud api'] = {
536536
test.equal(1, r.result.n);
537537
test.equal(0, r.matchedCount);
538538
test.ok(r.upsertedId != null);
539+
test.equal('ObjectID', r.upsertedId._bsontype);
540+
test.equal('ObjectID', r.upsertedId._id._bsontype);
539541

540542
db.collection('t3_2').updateOne({ c: 1 }
541543
, { $set: { a: 1 } }, function(err, r) {

0 commit comments

Comments
 (0)