summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTess Avitabile <tess.avitabile@mongodb.com>2017-04-04 13:14:40 -0400
committerTess Avitabile <tess.avitabile@mongodb.com>2017-04-04 13:15:20 -0400
commit6b4a0c3395a541543b8fd5abd9c853fd04b8e80f (patch)
tree395df173c6620f7fb9578256441fa3d32a9384a5 /src
parent83bc8601476096bdb666f825c3d516cb7af4cb35 (diff)
downloadmongo-6b4a0c3395a541543b8fd5abd9c853fd04b8e80f.tar.gz
SERVER-28576 Add arrayFilters parameter to update and findAndModify shell helpers
Diffstat (limited to 'src')
-rw-r--r--src/mongo/shell/bulk_api.js25
-rw-r--r--src/mongo/shell/collection.js17
-rw-r--r--src/mongo/shell/crud_api.js26
-rw-r--r--src/mongo/shell/explainable.js5
4 files changed, 69 insertions, 4 deletions
diff --git a/src/mongo/shell/bulk_api.js b/src/mongo/shell/bulk_api.js
index 16eb1687f60..a8ae57b92a7 100644
--- a/src/mongo/shell/bulk_api.js
+++ b/src/mongo/shell/bulk_api.js
@@ -683,6 +683,11 @@ var _bulk_api_module = (function() {
document.collation = currentOp.collation;
}
+ // Copy over the arrayFilters, if we have them.
+ if (currentOp.hasOwnProperty('arrayFilters')) {
+ document.arrayFilters = currentOp.arrayFilters;
+ }
+
// Clear out current Op
currentOp = null;
// Add the update document to the list
@@ -703,6 +708,11 @@ var _bulk_api_module = (function() {
document.collation = currentOp.collation;
}
+ // Copy over the arrayFilters, if we have them.
+ if (currentOp.hasOwnProperty('arrayFilters')) {
+ document.arrayFilters = currentOp.arrayFilters;
+ }
+
// Clear out current Op
currentOp = null;
// Add the update document to the list
@@ -767,6 +777,21 @@ var _bulk_api_module = (function() {
currentOp.collation = collationSpec;
return findOperations;
},
+
+ arrayFilters: function(filters) {
+ if (!collection.getMongo().hasWriteCommands()) {
+ throw new Error(
+ "cannot use arrayFilters if server does not support write commands");
+ }
+
+ if (collection.getMongo().writeMode() !== "commands") {
+ throw new Error("write mode must be 'commands' in order to use arrayFilters, " +
+ "but found write mode: " + collection.getMongo().writeMode());
+ }
+
+ currentOp.arrayFilters = filters;
+ return findOperations;
+ },
};
//
diff --git a/src/mongo/shell/collection.js b/src/mongo/shell/collection.js
index ea596a70625..4445b9383a4 100644
--- a/src/mongo/shell/collection.js
+++ b/src/mongo/shell/collection.js
@@ -504,7 +504,8 @@ DBCollection.prototype._validateUpdateDoc = function(doc) {
/**
* Does validation of the update args. Throws if the parse is not successful, otherwise
- * returns a document containing fields for query, obj, upsert, multi, and wc.
+ * returns a document containing fields for query, obj, upsert, multi, wc, collation, and
+ * arrayFilters.
*
* Throws if the arguments are invalid.
*/
@@ -516,6 +517,7 @@ DBCollection.prototype._parseUpdate = function(query, obj, upsert, multi) {
var wc = undefined;
var collation = undefined;
+ var arrayFilters = undefined;
// can pass options via object for improved readability
if (typeof(upsert) === "object") {
if (multi) {
@@ -528,6 +530,7 @@ DBCollection.prototype._parseUpdate = function(query, obj, upsert, multi) {
wc = opts.writeConcern;
upsert = opts.upsert;
collation = opts.collation;
+ arrayFilters = opts.arrayFilters;
}
// Normalize 'upsert' and 'multi' to booleans.
@@ -544,7 +547,8 @@ DBCollection.prototype._parseUpdate = function(query, obj, upsert, multi) {
"upsert": upsert,
"multi": multi,
"wc": wc,
- "collation": collation
+ "collation": collation,
+ "arrayFilters": arrayFilters
};
};
@@ -556,6 +560,7 @@ DBCollection.prototype.update = function(query, obj, upsert, multi) {
var multi = parsed.multi;
var wc = parsed.wc;
var collation = parsed.collation;
+ var arrayFilters = parsed.arrayFilters;
var result = undefined;
var startTime =
@@ -573,6 +578,10 @@ DBCollection.prototype.update = function(query, obj, upsert, multi) {
updateOp.collation(collation);
}
+ if (arrayFilters) {
+ updateOp.arrayFilters(arrayFilters);
+ }
+
if (multi) {
updateOp.update(obj);
} else {
@@ -594,6 +603,10 @@ DBCollection.prototype.update = function(query, obj, upsert, multi) {
throw new Error("collation requires use of write commands");
}
+ if (arrayFilters) {
+ throw new Error("arrayFilters requires use of write commands");
+ }
+
this._validateUpdateDoc(obj);
this.getMongo().update(this._fullName, query, obj, upsert, multi);
diff --git a/src/mongo/shell/crud_api.js b/src/mongo/shell/crud_api.js
index 99904fcb3ea..32ad1240b44 100644
--- a/src/mongo/shell/crud_api.js
+++ b/src/mongo/shell/crud_api.js
@@ -51,9 +51,11 @@ DBCollection.prototype.addIdIfNeeded = function(obj) {
*
* { insertOne: { document: { a: 1 } } }
*
-* { updateOne: { filter: {a:2}, update: {$set: {a:2}}, upsert:true, collation: {locale: "fr"} } }
+* { updateOne: { filter: {a:2}, update: {$set: {"a.$[i]":2}}, upsert:true, collation: {locale:
+* "fr"}, arrayFilters: [{i: 0}] } }
*
-* { updateMany: { filter: {a:2}, update: {$set: {a:2}}, upsert:true collation: {locale: "fr"} } }
+* { updateMany: { filter: {a:2}, update: {$set: {"a.$[i]":2}}, upsert:true collation: {locale:
+* "fr"}, arrayFilters: [{i: 0}] } }
*
* { deleteOne: { filter: {c:1}, collation: {locale: "fr"} } }
*
@@ -117,6 +119,10 @@ DBCollection.prototype.bulkWrite = function(operations, options) {
operation.collation(op.updateOne.collation);
}
+ if (op.updateOne.arrayFilters) {
+ operation.arrayFilters(op.updateOne.arrayFilters);
+ }
+
operation.updateOne(op.updateOne.update);
} else if (op.updateMany) {
if (!op.updateMany.filter) {
@@ -137,6 +143,10 @@ DBCollection.prototype.bulkWrite = function(operations, options) {
operation.collation(op.updateMany.collation);
}
+ if (op.updateMany.arrayFilters) {
+ operation.arrayFilters(op.updateMany.arrayFilters);
+ }
+
operation.update(op.updateMany.update);
} else if (op.replaceOne) {
if (!op.replaceOne.filter) {
@@ -551,6 +561,10 @@ DBCollection.prototype.updateOne = function(filter, update, options) {
op.collation(opts.collation);
}
+ if (opts.arrayFilters) {
+ op.arrayFilters(opts.arrayFilters);
+ }
+
op.updateOne(update);
try {
@@ -630,6 +644,10 @@ DBCollection.prototype.updateMany = function(filter, update, options) {
op.collation(opts.collation);
}
+ if (opts.arrayFilters) {
+ op.arrayFilters(opts.arrayFilters);
+ }
+
op.update(update);
try {
@@ -819,6 +837,10 @@ DBCollection.prototype.findOneAndUpdate = function(filter, update, options) {
cmd.collation = opts.collation;
}
+ if (opts.arrayFilters) {
+ cmd.arrayFilters = opts.arrayFilters;
+ }
+
// Set flags
cmd.upsert = (typeof opts.upsert == 'boolean') ? opts.upsert : false;
cmd.new = (typeof opts.returnNewDocument == 'boolean') ? opts.returnNewDocument : false;
diff --git a/src/mongo/shell/explainable.js b/src/mongo/shell/explainable.js
index 2d6c9c6b0dd..0dcc5c6d76e 100644
--- a/src/mongo/shell/explainable.js
+++ b/src/mongo/shell/explainable.js
@@ -198,6 +198,7 @@ var Explainable = (function() {
var upsert = parsed.upsert;
var multi = parsed.multi;
var collation = parsed.collation;
+ var arrayFilters = parsed.arrayFilters;
var bulk = this._collection.initializeOrderedBulkOp();
var updateOp = bulk.find(query);
@@ -210,6 +211,10 @@ var Explainable = (function() {
updateOp.collation(collation);
}
+ if (arrayFilters) {
+ updateOp.arrayFilters(arrayFilters);
+ }
+
if (multi) {
updateOp.update(obj);
} else {