Skip to content

Keep find_one_and_* options and returned documents as stored - #169

Merged
quinnj-bot[bot] merged 2 commits into
masterfrom
claude/find-and-modify-raw-copy
Nov 19, 2026
Merged

quinnj-bot[bot] merged 2 commits into
masterfrom
claude/find-and-modify-raw-copy

Conversation

@quinnj-bot

@quinnj-bot quinnj-bot Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Why

find_one_and_update, find_one_and_replace, and find_one_and_delete can modify the wrong document, and can throw after the server already applied the write.

  • Wrong document. These functions copied each option into the findAndModify command by reading it as a Julia value and writing it back. A sort document came back in Dict order. With options = BSON("sort" => BSON("""{"a": -1, "b": 1}""")), find picks highA but find_one_and_update updated lowB. All three functions picked lowB for both {a: -1, b: 1} and {b: 1, a: -1}.
  • Write applied, call throws. The returned document went through reply["value"], which gives a Dict, and was then converted back to BSON. If the stored document had a timestamp, regex, MinKey, or MaxKey field, the call threw MethodError: no method matching setindex!(::Mongoc.BSON, ::Mongoc.BSONUnsupported, ::String) after the server had already applied the update, replacement, or delete. For find_one_and_delete, the document was deleted and the caller never received it.
  • Changed document. Every returned document had its fields reordered (_id moved to the end). Strings were cut at an embedded NUL, binary subtypes became 0 (subtype 3 became 4), and duplicate keys collapsed.

What changed

  • Each option is copied into the command as raw BSON with bson_append_iter. This is the new internal helper copy_field!.
  • The value field of the reply is returned as a BSON copy of its bytes, or nothing when it is null. This is the new internal helper get_document.
  • New bindings: bson_append_iter, bson_iter_document, and bson_new_from_data.

CollectionSession methods call these same functions, so they are fixed too.

Verification

  • New testset "find one and modify passes options and documents through unchanged" in test/mongodb_tests.jl. On master it fails 6 of 9 assertions: 3 MethodErrors and 3 Evaluated: "lowB" == "highA". On this branch all 9 pass.
  • A differential harness inserts 21 documents, one per BSON type, plus nested order and duplicate keys. It compares each 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.
  • Full 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

🤖 Generated with Claude Code

quinnj and others added 2 commits November 19, 2026 04:03
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

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.29630% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.04%. Comparing base (4cdcae7) to head (92f0734).

Files with missing lines Patch % Lines
src/bson.jl 93.33% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@quinnj-bot
quinnj-bot Bot merged commit e2c8158 into master Nov 19, 2026
14 checks passed
@quinnj-bot
quinnj-bot Bot deleted the claude/find-and-modify-raw-copy branch November 19, 2026 11:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant