summaryrefslogtreecommitdiff
path: root/src/mongo/shell
diff options
context:
space:
mode:
authorZhihui Fan <yizhi.fzh@alibaba-inc.com>2020-02-04 08:09:01 +0800
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-04-22 16:47:37 +0000
commitbad7c538e7efbc996a6089e1569681edf24e6b33 (patch)
tree07aa2c5b48062ed36d147a682f06beeb4d6f9f69 /src/mongo/shell
parent17edea396d470d0ddc258674feba030ffafbffe8 (diff)
downloadmongo-bad7c538e7efbc996a6089e1569681edf24e6b33.tar.gz
SERVER-9306 Ability to temporarily forbid query optimizer from using index ("Hidden Index") SERVER-47275 Take over and complete Hidden Indexes PR
Co-authored-by: Ruoxin Xu <ruoxin.xu@mongodb.com>
Diffstat (limited to 'src/mongo/shell')
-rw-r--r--src/mongo/shell/collection.js34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/mongo/shell/collection.js b/src/mongo/shell/collection.js
index 86c0dcc1fc1..4ff9cc8a014 100644
--- a/src/mongo/shell/collection.js
+++ b/src/mongo/shell/collection.js
@@ -60,6 +60,11 @@ DBCollection.prototype.help = function() {
print("\tdb." + shortName + ".drop() drop the collection");
print("\tdb." + shortName + ".dropIndex(index) - e.g. db." + shortName +
".dropIndex( \"indexName\" ) or db." + shortName + ".dropIndex( { \"indexKey\" : 1 } )");
+ print("\tdb." + shortName + ".hideIndex(index) - e.g. db." + shortName +
+ ".hideIndex( \"indexName\" ) or db." + shortName + ".hideIndex( { \"indexKey\" : 1 } )");
+ print("\tdb." + shortName + ".unhideIndex(index) - e.g. db." + shortName +
+ ".unhideIndex( \"indexName\" ) or db." + shortName +
+ ".unhideIndex( { \"indexKey\" : 1 } )");
print("\tdb." + shortName + ".dropIndexes()");
print("\tdb." + shortName +
".ensureIndex(keypattern[,options]) - DEPRECATED, use createIndex() instead");
@@ -874,6 +879,35 @@ DBCollection.prototype.dropIndex = function(index) {
return res;
};
+/**
+ * Hide an index from the query planner.
+ */
+DBCollection.prototype._hiddenIndex = function(index, hidden) {
+ assert(index, "please specify index to hide");
+
+ // Need an extra check for array because 'Array' is an 'object', but not every 'object' is an
+ // 'Array'.
+ var indexField = {};
+ if (typeof index == "string") {
+ indexField = {name: index, hidden: hidden};
+ } else if (typeof index == "object") {
+ indexField = {keyPattern: index, hidden: hidden};
+ } else {
+ throw new Error("Index must be either the index name or the index specification document");
+ }
+ var cmd = {"collMod": this._shortName, index: indexField};
+ var res = this._db.runCommand(cmd);
+ return res;
+};
+
+DBCollection.prototype.hideIndex = function(index) {
+ return this._hiddenIndex(index, true);
+};
+
+DBCollection.prototype.unhideIndex = function(index) {
+ return this._hiddenIndex(index, false);
+};
+
DBCollection.prototype.getCollection = function(subName) {
return this._db.getCollection(this._shortName + "." + subName);
};