summaryrefslogtreecommitdiff
path: root/jstests/core/txns/find_and_modify_in_transaction.js
blob: 835d94fbd610b8ee762546fadca5a2c491b8f92c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Test transactions including find-and-modify
// @tags: [assumes_unsharded_collection, uses_transactions]
(function() {
"use strict";

const dbName = "test";
const collName = "find_and_modify_in_transaction";
const testDB = db.getSiblingDB(dbName);
const testColl = testDB[collName];

testDB.runCommand({drop: collName, writeConcern: {w: "majority"}});

assert.commandWorked(testDB.createCollection(testColl.getName(), {writeConcern: {w: "majority"}}));

const sessionOptions = {
    causalConsistency: false
};
const session = db.getMongo().startSession(sessionOptions);
const sessionDb = session.getDatabase(dbName);
const sessionColl = sessionDb[collName];

jsTest.log("Prepopulate the collection.");
assert.commandWorked(testColl.insert([{_id: 0, a: 0}, {_id: 1, a: 1}, {_id: 2, a: 2}],
                                     {writeConcern: {w: "majority"}}));

/***********************************************************************************************
 * Do a non-matching find-and-modify with remove.
 **********************************************************************************************/

jsTest.log("Do a non-matching find-and-modify with remove.");
session.startTransaction({writeConcern: {w: "majority"}});

// Do a findAndModify that affects no documents.
let res = sessionColl.findAndModify({query: {a: 99}, remove: true});
assert.eq(null, res);
let docs = sessionColl.find({}).toArray();
assert.sameMembers(docs, [{_id: 0, a: 0}, {_id: 1, a: 1}, {_id: 2, a: 2}]);

// Commit the transaction.
assert.commandWorked(session.commitTransaction_forTesting());

/***********************************************************************************************
 * Do a non-matching find-and-modify with update.
 **********************************************************************************************/

jsTest.log("Do a non-matching find-and-modify with update.");

session.startTransaction({writeConcern: {w: "majority"}});

res = sessionColl.findAndModify({query: {a: 99}, update: {$inc: {a: 100}}});
assert.eq(null, res);
docs = sessionColl.find({}).toArray();
assert.sameMembers(docs, [{_id: 0, a: 0}, {_id: 1, a: 1}, {_id: 2, a: 2}]);

// Commit the transaction.
assert.commandWorked(session.commitTransaction_forTesting());

/***********************************************************************************************
 * Do a matching find-and-modify with remove.
 **********************************************************************************************/

jsTest.log("Do a matching find-and-modify with remove.");

session.startTransaction({writeConcern: {w: "majority"}});

res = sessionColl.findAndModify({query: {a: 0}, remove: true});
assert.eq({_id: 0, a: 0}, res);
docs = sessionColl.find({}).toArray();
assert.sameMembers(docs, [{_id: 1, a: 1}, {_id: 2, a: 2}]);

// Commit the transaction.
assert.commandWorked(session.commitTransaction_forTesting());

/***********************************************************************************************
 * Do a matching find-and-modify with update, requesting the old doc.
 **********************************************************************************************/

jsTest.log("Do a matching find-and-modify with update, requesting the old doc.");
session.startTransaction({writeConcern: {w: "majority"}});

res = sessionColl.findAndModify({query: {a: 1}, update: {$inc: {a: 100}}});
assert.eq({_id: 1, a: 1}, res);
docs = sessionColl.find({}).toArray();
assert.sameMembers(docs, [{_id: 1, a: 101}, {_id: 2, a: 2}]);

// Commit the transaction.
assert.commandWorked(session.commitTransaction_forTesting());

/***********************************************************************************************
 * Do a matching find-and-modify with update, requesting the new doc.
 **********************************************************************************************/

jsTest.log("Do a matching find-and-modify with update, requesting the new doc.");
session.startTransaction({writeConcern: {w: "majority"}});

res = sessionColl.findAndModify({query: {a: 2}, update: {$inc: {a: 100}}, new: true});
assert.eq({_id: 2, a: 102}, res);
docs = sessionColl.find({}).toArray();
assert.sameMembers(docs, [{_id: 1, a: 101}, {_id: 2, a: 102}]);

// Commit the transaction.
assert.commandWorked(session.commitTransaction_forTesting());

/***********************************************************************************************
 * Do a matching find-and-modify with upsert, requesting the new doc.
 **********************************************************************************************/

jsTest.log("Do a matching find-and-modify with upsert, requesting the new doc.");
session.startTransaction({writeConcern: {w: "majority"}});

res =
    sessionColl.findAndModify({query: {_id: 2}, update: {$inc: {a: 100}}, upsert: true, new: true});
assert.eq({_id: 2, a: 202}, res);
docs = sessionColl.find({}).toArray();
assert.sameMembers(docs, [{_id: 1, a: 101}, {_id: 2, a: 202}]);

// Commit the transaction.
assert.commandWorked(session.commitTransaction_forTesting());

/***********************************************************************************************
 * Do a non-matching find-and-modify with upsert, requesting the old doc.
 **********************************************************************************************/

jsTest.log("Do a non-matching find-and-modify with upsert, requesting the old doc.");
session.startTransaction({writeConcern: {w: "majority"}});

res = sessionColl.findAndModify({query: {a: 3}, update: {$inc: {a: 100}}, upsert: true});
assert.eq(null, res);
docs = sessionColl.find({a: 103}, {_id: 0}).toArray();
assert.sameMembers(docs, [{a: 103}]);

// Commit the transaction.
assert.commandWorked(session.commitTransaction_forTesting());

/***********************************************************************************************
 * Do a non-matching find-and-modify with upsert, requesting the new doc.
 **********************************************************************************************/

jsTest.log("Do a non-matching find-and-modify with upsert, requesting the new doc.");
session.startTransaction({writeConcern: {w: "majority"}});
res = sessionColl.findAndModify({query: {a: 4}, update: {$inc: {a: 200}}, upsert: true, new: true});

const newdoc = res;
assert.eq(204, newdoc.a);
docs = sessionColl.find({a: 204}).toArray();
assert.sameMembers(docs, [newdoc]);

// Commit the transaction.
assert.commandWorked(session.commitTransaction_forTesting());
session.endSession();
}());