diff options
author | Scott Hernandez <scotthernandez@gmail.com> | 2013-07-12 12:13:08 -0400 |
---|---|---|
committer | Scott Hernandez <scotthernandez@gmail.com> | 2013-07-15 09:12:42 -0400 |
commit | 746dee394d9529b42b785d4c72c1187bc33c936f (patch) | |
tree | 8aa5453e2b1b6ef8c3aca6eb24bb812b2fecdd6d | |
parent | 4d95966d3ec97c148d887fd97265b55cb01309e1 (diff) | |
download | mongo-746dee394d9529b42b785d4c72c1187bc33c936f.tar.gz |
SERVER-7174: $push fix for missing dest
-rw-r--r-- | src/mongo/db/ops/modifier_push.cpp | 4 | ||||
-rw-r--r-- | src/mongo/db/ops/modifier_push_test.cpp | 32 |
2 files changed, 35 insertions, 1 deletions
diff --git a/src/mongo/db/ops/modifier_push.cpp b/src/mongo/db/ops/modifier_push.cpp index b9d6f58fbc7..488d1ef3b59 100644 --- a/src/mongo/db/ops/modifier_push.cpp +++ b/src/mongo/db/ops/modifier_push.cpp @@ -360,9 +360,11 @@ namespace mongo { } else if (status.isOK()) { + const bool destExists = (_preparedState->idxFound == + static_cast<int32_t>(_fieldRef.numParts()-1)); // If the path exists, we require the target field to be already an // array. - if (_preparedState->elemFound.getType() != Array) { + if (destExists && _preparedState->elemFound.getType() != Array) { return Status(ErrorCodes::BadValue, "can only $push into arrays"); } diff --git a/src/mongo/db/ops/modifier_push_test.cpp b/src/mongo/db/ops/modifier_push_test.cpp index 4204a8007bf..0a8c46b670a 100644 --- a/src/mongo/db/ops/modifier_push_test.cpp +++ b/src/mongo/db/ops/modifier_push_test.cpp @@ -588,6 +588,38 @@ namespace { ASSERT_EQUALS(fromjson("{$set: {a: [{b:0},{b:1}]}}"), logDoc); } + TEST(SimpleObjMod, PrepareApplyDotted) { + Document doc(fromjson("{ _id : 1 , " + " question : 'a', " + " choices : { " + " first : { choice : 'b' }, " + " second : { choice : 'c' } }" + "}")); + Mod pushMod(fromjson("{$push: {'choices.first.votes': 1}}")); + + ModifierInterface::ExecInfo execInfo; + ASSERT_OK(pushMod.prepare(doc.root(), "", &execInfo)); + + ASSERT_EQUALS(execInfo.fieldRef[0]->dottedField(), "choices.first.votes"); + ASSERT_FALSE(execInfo.inPlace); + ASSERT_FALSE(execInfo.noOp); + + ASSERT_OK(pushMod.apply()); + ASSERT_EQUALS(fromjson( "{ _id : 1 , " + " question : 'a', " + " choices : { " + " first : { choice : 'b', votes: [1]}, " + " second : { choice : 'c' } }" + "}"), + doc); + + Document logDoc; + ASSERT_OK(pushMod.log(logDoc.root())); + ASSERT_EQUALS(countChildren(logDoc.root()), 1u); + ASSERT_EQUALS(fromjson("{$set: {'choices.first.votes':[1]}}"), logDoc); + } + + // // $pushAll Variation // |