Repository navigation
Keep find_one_and_* options and returned documents as stored - #169
Merged
Merged
Conversation
The sort option's key order decides which document is modified, and the returned document must not be re-encoded through Julia values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
find_one_and_update, find_one_and_replace, and find_one_and_delete copied options into the command by decoding them to Julia values and encoding them again, and returned reply["value"] converted from a Dict. A multi-key sort came back in Dict order, so the server modified a different document than the caller asked for. Returned documents lost their field order, embedded NULs, and binary subtypes, and a field of a type Mongoc cannot encode (timestamp, regex, MinKey) threw a MethodError after the server had already applied the write. Copy option elements with bson_append_iter and return the embedded reply document as a BSON copy of its bytes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #169 +/- ##
==========================================
+ Coverage 85.84% 86.04% +0.20%
==========================================
Files 11 11
Lines 1674 1691 +17
==========================================
+ Hits 1437 1455 +18
+ Misses 237 236 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
find_one_and_update,find_one_and_replace, andfind_one_and_deletecan modify the wrong document, and can throw after the server already applied the write.findAndModifycommand by reading it as a Julia value and writing it back. A sort document came back inDictorder. Withoptions = BSON("sort" => BSON("""{"a": -1, "b": 1}""")),findpickshighAbutfind_one_and_updateupdatedlowB. All three functions pickedlowBfor both{a: -1, b: 1}and{b: 1, a: -1}.reply["value"], which gives aDict, and was then converted back toBSON. If the stored document had a timestamp, regex, MinKey, or MaxKey field, the call threwMethodError: no method matching setindex!(::Mongoc.BSON, ::Mongoc.BSONUnsupported, ::String)after the server had already applied the update, replacement, or delete. Forfind_one_and_delete, the document was deleted and the caller never received it._idmoved to the end). Strings were cut at an embedded NUL, binary subtypes became 0 (subtype 3 became 4), and duplicate keys collapsed.What changed
bson_append_iter. This is the new internal helpercopy_field!.valuefield of the reply is returned as aBSONcopy of its bytes, ornothingwhen it is null. This is the new internal helperget_document.bson_append_iter,bson_iter_document, andbson_new_from_data.CollectionSessionmethods call these same functions, so they are fixed too.Verification
test/mongodb_tests.jl. On master it fails 6 of 9 assertions: 3MethodErrors and 3Evaluated: "lowB" == "highA". On this branch all 9 pass.find_one_and_*return with the stored document's canonical Extended JSON. On master, 1 of 65 checks passed. On this branch, all 65 pass.Pkg.test()passes on Julia 1.12.6 with MongoC_jll 1.30.8 and MongoDB 5.0.33. BSON 173/173, MongoDB 264/264.Self-review
copy_field!matches keys exactly, the same asgetindex. The old code usedhaskey, which follows dotted paths. No option key contains a dot.valuefield still throwsKeyError, as before.BSONowns its copy, so it does not depend on the lifetime ofreply.Dictconstants. MongoDB requires onlyfindAndModifyto come first, and it still does.src/. If Alternative PR to address uniqueness ordering and properties, introducing handle(bson) #152 lands first, the new.handleuses incopy_field!andget_documentshould becomehandle(...), because Alternative PR to address uniqueness ordering and properties, introducing handle(bson) #152 routes.handlethroughgetproperty.🤖 Generated with Claude Code