Skip to content

Commit 93d8298

Browse files
SERVER-12852: validate update+upsert->insert doc
1 parent 7ee0b02 commit 93d8298

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

jstests/upsert3.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
t = db.upsert3;
33
t.drop();
44

5+
//make sure we validate query
6+
t.update( {a: {"a.a": 1}} , {$inc: {y: 1}} , true );
7+
assert.gleError(db, function(gle) {
8+
return "a.a.a-1 - " + tojson(gle) + " doc:" + tojson(t.findOne()) });
9+
10+
t.update( {a: {$a: 1}} , {$inc: {y: 1}} , true );
11+
assert.gleError(db, function(gle) {
12+
return "a.$a-1 - " + tojson(gle) + " doc:" + tojson(t.findOne()) });
13+
514
// make sure the new _id is not duplicated
615
t.update( {"a.b": 1, a: {a: 1, b: 1}} , {$inc: {y: 1}} , true );
716
assert.gleError(db, function(gle) {

src/mongo/db/ops/update.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,9 @@ namespace mongo {
795795
// creates the base of the update for the inserterd doc (because upsert was true)
796796
if (cq) {
797797
uassertStatusOK(driver->populateDocumentWithQueryFields(cq, doc));
798+
// Validate the base doc, as taken from the query -- no fields means validate all.
799+
FieldRefSet noFields;
800+
uassertStatusOK(validate(BSONObj(), noFields, doc, NULL, driver->modOptions()));
798801
if (!driver->isDocReplacement()) {
799802
opDebug->fastmodinsert = true;
800803
// We need all the fields from the query to compare against for validation below.
@@ -828,6 +831,7 @@ namespace mongo {
828831
if (lifecycle)
829832
immutableFields = lifecycle->getImmutableFields();
830833

834+
// This will only validate the modified fields if not a replacement.
831835
uassertStatusOK(validate(original,
832836
updatedFields,
833837
doc,

0 commit comments

Comments
 (0)