mongodb-erlang icon indicating copy to clipboard operation
mongodb-erlang copied to clipboard

Feature: Support findOneAndXXX operations

Open ehlertjd opened this issue 7 years ago • 7 comments

Should support the following operations, with the returnNewDocument option:

  • findOneAndDelete
  • findOneAndReplace
  • findOneAndUpdate

See: https://docs.mongodb.com/manual/reference/command/findAndModify/

ehlertjd avatar Jul 21 '17 14:07 ehlertjd

Hi, ehlertjd Not all API is exposed directly, but you can call it through mc_worker_api:command/2/3.

comtihon avatar Jul 23 '17 13:07 comtihon

@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?

InoMurko avatar Aug 24 '17 20:08 InoMurko

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 :(

comtihon avatar Aug 29 '17 08:08 comtihon

Me neither. if you find them, please share!

InoMurko avatar Aug 30 '17 20:08 InoMurko

Any updates on this. I am looking for using findAndModify?

immnas avatar Mar 08 '22 14:03 immnas

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.

comtihon avatar Mar 09 '22 12:03 comtihon

Hi, I have try following options but i am getting error every time.

  1. 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.">>}}

  1. 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">>}}

  1. 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.">>}}

  1. 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.">>}}

immnas avatar Mar 09 '22 13:03 immnas