Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

SERVER-7452 Add an upsert flag to the applyOps command

  • Loading branch information...
commit 552ccd85cfc6a5145996a8269eeb8b778d6bcaf8 1 parent 9541d2b
@renctan renctan authored
Showing with 18 additions and 2 deletions.
  1. +15 −1 jstests/apply_ops1.js
  2. +3 −1 src/mongo/db/oplog.cpp
View
16 jstests/apply_ops1.js
@@ -62,4 +62,18 @@ res = db.runCommand( { applyOps :
} );
assert.eq( true, res.results[0], "B5" );
-assert.eq( false, res.results[1], "B6" );
+assert.eq( true, res.results[1], "B6" );
+
+// Test update on non-existing document will be treated as an upsert
+res = db.runCommand({ applyOps: [{ "op" : "u", "ns" : t.getFullName(),
+ "o2": { _id: 7 }, "o": { _id: 7, x: 1 }}] });
+
+assert(res.results[0]);
+assert(t.findOne({ _id: 7 }) != null);
+
+res = db.runCommand({ applyOps: [{ "op" : "u", "ns" : t.getFullName(),
+ "o2": { _id: 8 }, "o": { _id: 8, x: 1 }}], alwaysUpsert: false });
+
+assert(!res.results[0]);
+assert(t.findOne({ _id: 8 }) == null);
+
View
4 src/mongo/db/oplog.cpp
@@ -923,13 +923,15 @@ namespace mongo {
BSONObjIterator i( ops );
BSONArrayBuilder ab;
+ const bool alwaysUpsert = cmdObj.hasField("alwaysUpsert") ?
+ cmdObj["alwaysUpsert"].trueValue() : true;
while ( i.more() ) {
BSONElement e = i.next();
const BSONObj& temp = e.Obj();
Client::Context ctx( temp["ns"].String() ); // this handles security
- bool failed = applyOperation_inlock( temp , false );
+ bool failed = applyOperation_inlock(temp, false, alwaysUpsert);
ab.append(!failed);
if ( failed )
errors++;
Please sign in to comment.
Something went wrong with that request. Please try again.