Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/bson.jl
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,19 @@ function Base.get(document::BSON, key::AbstractString, default::Any)
return bson_iter_init_find(iter_ref, document.handle, key) ? get_value(iter_ref) : default
end

# Returns the embedded document `document[key]` as a `BSON` copy of its bytes, or `nothing`
# when the value is null. Unlike `getindex`, it keeps field order, duplicate keys, and every type.
function get_document(document::BSON, key::AbstractString) :: Union{Nothing, BSON}
iter_ref = Ref{BSONIter}()
bson_iter_init_find(iter_ref, document.handle, key) || throw(KeyError(key))
bson_iter_type(iter_ref) == BSON_TYPE_NULL && return nothing
length_ref = Ref{UInt32}()
data_ref = Ref{Ptr{UInt8}}()
bson_iter_document(iter_ref, length_ref, data_ref)
data_ref[] == C_NULL && error("Value for key $key is not a document.")
return BSON(bson_new_from_data(data_ref[], length_ref[]))
end

"""
get_as_bson_value(doc, key) :: BSONValue

Expand All @@ -652,6 +665,17 @@ end
# Write values to BSON
#

# Appends `src[src_key]` to `dst` as `dst_key`, copying the element's bytes without decoding it.
# Does nothing when `src` has no `src_key`.
function copy_field!(dst::BSON, dst_key::AbstractString, src::BSON, src_key::AbstractString)
iter_ref = Ref{BSONIter}()
bson_iter_init_find(iter_ref, src.handle, src_key) || return nothing
if !bson_append_iter(dst.handle, dst_key, -1, iter_ref)
error("Couldn't append $dst_key to BSON document.")
end
nothing
end

function Base.setindex!(document::BSON, value::BSONObjectId, key::AbstractString)
ok = bson_append_oid(document.handle, key, -1, value)
if !ok
Expand Down
16 changes: 16 additions & 0 deletions src/c_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,20 @@ function bson_append_null(bson_document::Ptr{Cvoid}, key::AbstractString, key_le
bson_document, key, key_length)
end

function bson_append_iter(bson_document::Ptr{Cvoid}, key::AbstractString, key_length::Int, iter_ref::Ref{BSONIter})
ccall((:bson_append_iter, libbson), Bool,
(Ptr{Cvoid}, Cstring, Cint, Ref{BSONIter}),
bson_document, key, key_length, iter_ref)
end

function bson_new()
ccall((:bson_new, libbson), Ptr{Cvoid}, ())
end

function bson_new_from_data(data::Ptr{UInt8}, len::Integer)
ccall((:bson_new_from_data, libbson), Ptr{Cvoid}, (Ptr{UInt8}, Csize_t), data, len)
end

function bson_new_from_json(data::String, len::Int=-1)
ccall((:bson_new_from_json, libbson), Ptr{Cvoid},
(Ptr{UInt8}, Cssize_t, Ptr{Cvoid}),
Expand Down Expand Up @@ -234,6 +244,12 @@ function bson_iter_timestamp(iter_ref::Ref{BSONIter}) :: BSONTimestamp
return BSONTimestamp(timestamp[], increment[])
end

function bson_iter_document(iter_ref::Ref{BSONIter}, length_ref::Ref{UInt32}, data_ref::Ref{Ptr{UInt8}})
ccall((:bson_iter_document, libbson), Cvoid,
(Ref{BSONIter}, Ref{UInt32}, Ref{Ptr{UInt8}}),
iter_ref, length_ref, data_ref)
end

function bson_iter_recurse(iter_ref::Ref{BSONIter}, child_iter_ref::Ref{BSONIter})
ccall((:bson_iter_recurse, libbson), Bool, (Ref{BSONIter}, Ref{BSONIter}), iter_ref, child_iter_ref)
end
Expand Down
20 changes: 6 additions & 14 deletions src/collection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -596,14 +596,10 @@ function find_one_and_delete(collection::Collection, bson_filter::BSON;

if !isnothing(options)
for (opt_key, comm_key) in FIND_ONE_AND_DELETE_FIELDS
if haskey(options, opt_key)
command_bson[comm_key] = options[opt_key]
end
copy_field!(command_bson, comm_key, options, opt_key)
end

if haskey(options, "sessionId")
options_bson["sessionId"] = options["sessionId"]
end
copy_field!(options_bson, "sessionId", options, "sessionId")
end

reply = write_command(
Expand All @@ -612,7 +608,7 @@ function find_one_and_delete(collection::Collection, bson_filter::BSON;
options=options_bson,
)

return reply["value"]
return get_document(reply, "value")
end

const FIND_ONE_AND_MODIFY_FIELDS = Dict(
Expand Down Expand Up @@ -640,14 +636,10 @@ function _find_one_and_modify(collection::Collection, bson_filter::BSON, bson_mo

if !isnothing(options)
for (opt_key, comm_key) in FIND_ONE_AND_MODIFY_FIELDS
if haskey(options, opt_key)
command_bson[comm_key] = options[opt_key]
end
copy_field!(command_bson, comm_key, options, opt_key)
end

if haskey(options, "sessionId")
options_bson["sessionId"] = options["sessionId"]
end
copy_field!(options_bson, "sessionId", options, "sessionId")
end

reply = write_command(
Expand All @@ -656,7 +648,7 @@ function _find_one_and_modify(collection::Collection, bson_filter::BSON, bson_mo
options=options_bson,
)

return reply["value"]
return get_document(reply, "value")
end

"""
Expand Down
33 changes: 33 additions & 0 deletions test/mongodb_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,39 @@ const DB_NAME = "mongoc"
Mongoc.drop(collection)
end

@testset "find one and modify passes options and documents through unchanged" begin
collection = client[DB_NAME]["find_one_and_modify_raw"]
stored = Mongoc.BSON("""{ "_id" : 1, "z" : { "y" : 1, "b" : [ { "\$minKey" : 1 } ] },
"ts" : { "\$timestamp" : { "t" : 1, "i" : 2 } }, "re" : { "\$regex" : "^a", "\$options" : "i" } }""")
Mongoc.insert_one(collection, stored)
filter = Mongoc.BSON("_id" => 1)
current() = Mongoc.as_json(Mongoc.find_one(collection, filter))

before = current()
@test Mongoc.as_json(Mongoc.find_one_and_update(collection, filter,
Mongoc.BSON("""{ "\$set" : { "n" : 1 } }"""))) == before
before = current()
@test Mongoc.as_json(Mongoc.find_one_and_replace(collection, filter, stored)) == before
before = current()
@test Mongoc.as_json(Mongoc.find_one_and_delete(collection, filter)) == before

# The sort keys keep their order: { a : -1, b : 1 } and { b : 1, a : -1 } pick different documents.
ops = [
options -> Mongoc.find_one_and_update(collection, Mongoc.BSON(),
Mongoc.BSON("""{ "\$set" : { "n" : 1 } }"""); options),
options -> Mongoc.find_one_and_replace(collection, Mongoc.BSON(), Mongoc.BSON("n" => 1); options),
options -> Mongoc.find_one_and_delete(collection, Mongoc.BSON(); options),
]
for (sort, expected) in ["""{ "a" : -1, "b" : 1 }""" => "highA", """{ "b" : 1, "a" : -1 }""" => "lowB"], op in ops
Mongoc.delete_many(collection, Mongoc.BSON())
Mongoc.insert_one(collection, Mongoc.BSON("""{ "_id" : "lowB", "a" : 1, "b" : 1 }"""))
Mongoc.insert_one(collection, Mongoc.BSON("""{ "_id" : "highA", "a" : 2, "b" : 2 }"""))
@test op(Mongoc.BSON("sort" => Mongoc.BSON(sort)))["_id"] == expected
end

Mongoc.drop(collection)
end

@testset "command" begin
database = client[DB_NAME]
collection_name = "index_collection"
Expand Down