summaryrefslogtreecommitdiff
path: root/jstests
diff options
context:
space:
mode:
Diffstat (limited to 'jstests')
-rw-r--r--jstests/core/index_type_change.js37
-rw-r--r--jstests/core/set_type_change.js26
2 files changed, 63 insertions, 0 deletions
diff --git a/jstests/core/index_type_change.js b/jstests/core/index_type_change.js
new file mode 100644
index 00000000000..0c95828ae57
--- /dev/null
+++ b/jstests/core/index_type_change.js
@@ -0,0 +1,37 @@
+/**
+ * Tests that replacing a document with an equivalent document with different types for the fields
+ * will update the index entries associated with that document.
+ */
+
+load("jstests/libs/analyze_plan.js"); // For 'isIndexOnly'.
+
+(function() {
+ "use strict";
+
+ var coll = db.index_type_change;
+ coll.drop();
+ assert.commandWorked(coll.ensureIndex({a: 1}));
+
+ assert.writeOK(coll.insert({a: 2}));
+ assert.eq(1, coll.find({a: {$type: "double"}}).itcount());
+
+ var newVal = new NumberLong(2);
+ var res = coll.update({}, {a: newVal}); // Replacement update.
+ assert.writeOK(res);
+ assert.eq(res.nMatched, 1);
+ if (coll.getMongo().writeMode() == "commands")
+ assert.eq(res.nModified, 1);
+
+ // Make sure it actually changed the type.
+ assert.eq(1, coll.find({a: {$type: "long"}}).itcount());
+
+ // Now use a covered query to ensure the index entry has been updated.
+
+ // First make sure it's actually using a covered index scan.
+ var explain = coll.explain().find({a: 2}, {_id: 0, a: 1});
+ assert(isIndexOnly(explain));
+
+ var updated = coll.findOne({a: 2}, {_id: 0, a: 1});
+
+ assert(updated.a instanceof NumberLong, "Index entry did not change type");
+})();
diff --git a/jstests/core/set_type_change.js b/jstests/core/set_type_change.js
new file mode 100644
index 00000000000..1f24a7c953a
--- /dev/null
+++ b/jstests/core/set_type_change.js
@@ -0,0 +1,26 @@
+/**
+ * Tests that using the $set update modifier to change only the type of a field will actually update
+ * the document, including any relevant indices.
+ */
+
+load("jstests/libs/analyze_plan.js"); // For 'isIndexOnly'.
+
+(function() {
+ "use strict";
+
+ var coll = db.set_type_change;
+ coll.drop();
+ assert.commandWorked(coll.ensureIndex({a: 1}));
+
+ assert.writeOK(coll.insert({a: 2}));
+
+ var newVal = new NumberLong(2);
+ var res = coll.update({}, {$set: {a: newVal}});
+ assert.eq(res.nMatched, 1);
+ if (coll.getMongo().writeMode() == "commands")
+ assert.eq(res.nModified, 1);
+
+ // Make sure it actually changed the type.
+ var updated = coll.findOne();
+ assert(updated.a instanceof NumberLong, "$set did not update type of value: " + updated.a);
+})();