diff options
author | Bernard Gorman <bernard.gorman@gmail.com> | 2019-05-13 20:02:34 +0100 |
---|---|---|
committer | Bernard Gorman <bernard.gorman@gmail.com> | 2019-05-19 11:25:13 +0100 |
commit | 209ea38bcc2cfb68e0374e14814fc18461cebc58 (patch) | |
tree | 8bd4b6b66901c35b6514918d1c70032f8dc17f42 /jstests/noPassthrough/update_now_clustertime_replset.js | |
parent | b7a30e03816491a43887ed69a99b95481e83f0fd (diff) | |
download | mongo-209ea38bcc2cfb68e0374e14814fc18461cebc58.tar.gz |
SERVER-40407 Add support for $$NOW and $$CLUSTER_TIME in the findAndModify command
Diffstat (limited to 'jstests/noPassthrough/update_now_clustertime_replset.js')
-rw-r--r-- | jstests/noPassthrough/update_now_clustertime_replset.js | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/jstests/noPassthrough/update_now_clustertime_replset.js b/jstests/noPassthrough/update_now_clustertime_replset.js index 6ceb002e5d2..06ba7db2ce6 100644 --- a/jstests/noPassthrough/update_now_clustertime_replset.js +++ b/jstests/noPassthrough/update_now_clustertime_replset.js @@ -166,5 +166,80 @@ assert.eq(result.ctime5, result.ctime6); } + // Test that $$NOW and $$CLUSTER_TIME can be used in a findAndModify query and update. + let returnedDoc = coll.findAndModify({ + query: { + $expr: { + $and: [ + {$lt: ["$_id", {$min: [_idMidpoint, "$$NOW"]}]}, + {$gt: ["$$CLUSTER_TIME", "$insertClusterTime"]} + ] + } + }, + update: [{$addFields: {nowFAM: "$$NOW", ctimeFAM: "$$CLUSTER_TIME"}}], + sort: {_id: 1}, + new: true + }); + assert(returnedDoc.nowFAM instanceof Date); + assert(returnedDoc.ctimeFAM instanceof Timestamp); + assert.gt(returnedDoc.nowFAM, returnedDoc.now4); + assert.gt(returnedDoc.ctimeFAM, returnedDoc.ctime4); + + results = coll.find({nowFAM: {$exists: true}, ctimeFAM: {$exists: true}}).toArray(); + assert.eq(results.length, 1); + assert.docEq(results[0], returnedDoc); + + // Test that $$NOW and $$CLUSTER_TIME can be used in a findAndModify upsert. + returnedDoc = coll.findAndModify({ + query: {fieldDoesNotExist: {$exists: true}}, + update: + [{$addFields: {_id: "$$NOW", nowFAMUpsert: "$$NOW", ctimeFAMUpsert: "$$CLUSTER_TIME"}}], + sort: {_id: 1}, + upsert: true, + new: true + }); + assert(returnedDoc.nowFAMUpsert instanceof Date); + assert(returnedDoc.ctimeFAMUpsert instanceof Timestamp); + + assert.eq(coll.find().itcount(), numDocs + 1); + results = coll.find({nowFAMUpsert: {$exists: true}, ctimeFAMUpsert: {$exists: true}}).toArray(); + assert.eq(results.length, 1); + assert.docEq(results[0], returnedDoc); + + // Test that $$NOW and $$CLUSTER_TIME can be used in a findAndModify delete. + returnedDoc = coll.findAndModify({ + query: { + nowFAMUpsert: {$exists: true}, + ctimeFAMUpsert: {$exists: true}, + $expr: { + $and: [ + {$lt: ["$nowFAMUpsert", "$$NOW"]}, + {$gt: ["$$CLUSTER_TIME", "$ctimeFAMUpsert"]} + ] + } + }, + sort: {_id: 1}, + remove: true + }); + assert.eq(coll.find({nowFAMUpsert: {$exists: true}}).itcount(), 0); + assert.eq(coll.find().itcount(), numDocs); + assert.neq(returnedDoc, null); + + // Test that we can explain() a findAndModify command that uses $$NOW and $$CLUSTER_TIME. + assert.commandWorked(coll.explain().findAndModify({ + query: { + $expr: { + $and: [ + {$lt: ["$_id", {$min: [_idMidpoint, "$$NOW"]}]}, + {$gt: ["$$CLUSTER_TIME", "$insertClusterTime"]} + ] + } + }, + update: + [{$addFields: {explainDoesNotWrite1: "$$NOW", explainDoesNotWrite2: "$$CLUSTER_TIME"}}], + sort: {_id: 1}, + new: true + })); + rst.stopSet(); }()); |