benoitc / couchbeam

Apache CouchDB client in Erlang
Other
242 stars 114 forks source link

Trying to delete all docs, not working #75

Closed tleyden closed 12 years ago

tleyden commented 12 years ago

I'm trying to delete all docs with this function, and it doesn't seem to be working. (the docs are still in the db)

   fun(DocParam) ->
      DocId = couchbeam_doc:get_value("id", DocParam),
      io:format("doc id is ~p~n", [DocId]),
      DeleteResult = couchbeam:delete_doc(Db, DocParam),
      io:format("delete result is ~p~n", [DeleteResult]),
      DocExists = couchbeam:doc_exists(Db, DocId),
      io:format("doc exists ~p~n", [DocExists])
   end

Here's the entire shell history with output:

 1: couchbeam:start()
-> ok
2: Host = "localhost",
   Port = 5984,
   Prefix = "",
   Options = [],
   S = couchbeam:server_connection(Host, Port, Prefix, Options)
-> {server,"localhost",5984,[],[]}
3: {ok,_Version} = couchbeam:server_info(S)
-> {ok,{[{<<"couchdb">>,<<"Welcome">>},
     {<<"version">>,<<"1.2.0">>}]}}
4: DBOptions = [],
   {ok,Db} = couchbeam:open_db(S, "sig-retailer-model", DBOptions)
-> {ok,{db,{server,"localhost",5984,[],[]},
       "sig-retailer-model",[]}}
5: Options = [include_docs],
   {ok,AllDocs} = couchbeam_view:fetch(Db, all_docs, Options)
-> {'EXIT',{{badmatch,[include_docs]},[{erl_eval,expr,3,[]}]}}
6: OptionsAllDocs = [include_docs],
   {ok,AllDocs} = couchbeam_view:fetch(Db, all_docs, OptionsAllDocs)
-> {ok,[{[{<<"id">>,<<"9c034e3fdc006e266f879c821a7afecc">>},
      {<<"key">>,<<"9c034e3fdc006e266f879c821a7afecc">>},
      {<<"value">>,
       {[{<<"rev">>,<<"1-4c6114c65e295552ab1019e2b046b10e">>}]}},
      {<<"doc">>,
       {[{<<"_id">>,<<"9c034e3fdc006e266f879c821a7afecc">>},
     {<<"_rev">>,<<"1-4c6114c65e295552ab1019e2b046b10e">>},
     {<<"foo">>,<<"bar">>}]}}]}]}
7: DeleteDocsFun =
   fun(DocParam) ->
      DocId = couchbeam_doc:get_value("id", DocParam),
      io:format("doc id is ~p~n", [DocId]),
      DeleteResult = couchbeam:delete_doc(Db, DocParam),
      io:format("delete result is ~p~n", [DeleteResult]),
      DocExists = couchbeam:doc_exists(Db, DocId),
      io:format("doc exists ~p~n", [DocExists])
   end
-> #Fun<erl_eval.6.82930912>
8: lists:map(DeleteDocsFun, AllDocs)
doc id is <<"9c034e3fdc006e266f879c821a7afecc">>
delete result is {ok,[{[{<<"ok">>,true},
                    {<<"id">>,<<"9c034e3fdc006e266f879c821a7b0e1d">>},
                    {<<"rev">>,
                     <<"1-bdacf03ae718d94b2cf0476ab9efb467">>}]}]}
doc exists true
-> [ok]
ok

When I look at the db in futon, that doc is still there.

Any ideas on what I'm doing wrong?

kuloshius commented 12 years ago

Not tested. 7: DeleteDocsFun = fun(DocParam) -> DocId = couchbeam_doc:get_value("id", DocParam), io:format("doc id is ~p~n", [DocId]), Doc = couchbeam_doc:get_value("doc", DocParam), io:format("the actual document is ~p~n",[Doc]), DeleteResult = couchbeam:delete_doc(Db, Doc), io:format("delete result is ~p~n", [DeleteResult]), DocExists = couchbeam:doc_exists(Db, DocId), io:format("doc exists ~p~n", [DocExists]) end -> #Fun

To be able to delete the document form Db you have to pass a JSON as an argument which must have _id and _rev in it. For example {[{<<"_id">>, <<"some id">>}, {<<"_rev">>, <<"the revision number">>}]}. In your example you were passing {[{<<"id">>, <<"some id">>},{<<"key">>, <<"some id">>}, {<<"value">>, {[{<<"rev">>, <<"the revision number">>}]}}, {<<"doc">>, DocJSON}]}. So what we need to do is to get the value of <<"doc">> and pass it as an argument when deleting it from Db.

I hope I make sense ;-)

Cheers

tleyden commented 12 years ago

Yes, that makes sense.

I tried this approach and it worked.

DeleteDocsFun = fun(DocWrapper) -> DocId = couchbeam_doc:get_value("id", DocWrapper), io:format("doc id is ~p~n", [DocId]), {ok, DocFetched} = couchbeam:open_doc(Db, DocId), DeleteResult = couchbeam:delete_doc(Db, DocFetched), io:format("delete result is ~p~n", [DeleteResult]), DocExists = couchbeam:doc_exists(Db, DocId), io:format("doc exists ~p~n", [DocExists]) end.