mongodb-erlang
mongodb-erlang copied to clipboard
Feature: Support findOneAndXXX operations
Should support the following operations, with the returnNewDocument option:
- findOneAndDelete
- findOneAndReplace
- findOneAndUpdate
See: https://docs.mongodb.com/manual/reference/command/findAndModify/
Hi, ehlertjd
Not all API is exposed directly, but you can call it through mc_worker_api:command/2/3
.
@comtihon are https://github.com/comtihon/mongodb-erlang/blob/1fff1e803fe79fe8019f78ba1202f665a71c915e/src/api/mc_worker_api.erl#L76 <<"update">>and <<"updates">> bins important for the protocol?
how to they fit into findAndModify?
Hi, @InoMurko yes, they are important. It is a part of protocol. Its a pity, I can't find documentation link, as it changed again :(
Me neither. if you find them, please share!
Any updates on this. I am looking for using findAndModify?
Hi, I haven't personally checked it, but it should work if you specify the command. F.e. here aggregate operation is being used:
mc_worker_api:command(Connection,
{<<"aggregate">>, Collection,
<<"pipeline">>, [{<<"$match">>, {<<"key">>, <<"test">>}}, {<<"$sort">>, {<<"tag">>, 1}}],
<<"cursor">>, {<<"batchSize">>, 10}})
I believe findOneAndXXX
can also be send like this.
Hi, I have try following options but i am getting error every time.
- mc_worker_api:command(Connection, {<<"findAndModify">>, Collection, <<"update">>, [#{<<"query">> => #{ <<"category">> => <<"cafe">>, <<"status">> => <<"a">> }, <<"sort">> => #{<<"category">> => 1}, <<"update">> => #{ <<"$set">> => #{ <<"status">> => <<"Updated">> }}}]}).
{false,#{<<"code">> => 40323,<<"codeName">> => <<"Location40323">>, <<"errmsg">> => <<"A pipeline stage specification object must contain exactly one field.">>}}
- mc_worker_api:command(Connection, {<<"findAndModify">>, Collection, [#{<<"query">> => #{ <<"category">> => <<"cafe">>, <<"status">> => <<"a">> }, <<"sort">> => #{<<"category">> => 1}, <<"update">> => #{ <<"$set">> => #{ <<"status">> => <<"Updated">> }}}]}).
{false,#{<<"code">> => 9,<<"codeName">> => <<"FailedToParse">>, <<"errmsg">> => <<"Either an update or remove=true must be specified">>}}
- mc_worker_api:command(Connection, {<<"findAndModify">>, Collection, <<"update">>, #{<<"query">> => #{ <<"category">> => <<"cafe">>, <<"status">> => <<"a">> }, <<"sort">> => #{<<"category">> => 1}, <<"update">> => #{ <<"$set">> => #{ <<"status">> => <<"Updated">> }}}}).
{false,#{<<"code">> => 52, <<"codeName">> => <<"DollarPrefixedFieldName">>, <<"errmsg">> => <<"The dollar ($) prefixed field '$set' in 'update.$set' is not valid for storage.">>}}
- mc_worker_api:command(Connection, {<<"findAndModify">>, Collection, <<"updates">>, #{<<"query">> => #{ <<"category">> => <<"cafe">>, <<"status">> => <<"a">> }, <<"sort">> => #{<<"category">> => 1}, <<"update">> => #{ <<"$set">> => #{ <<"status">> => <<"Updated">> }}}}).
{false,#{<<"code">> => 51177,<<"codeName">> => <<"Location51177">>, <<"errmsg">> => <<"BSON field 'updates' is an unknown field.">>}}