Skip to content

Instantly share code, notes, and snippets.

@koolquark
Created December 2, 2018 10:05
Show Gist options
  • Save koolquark/4ae66ad88e0a1cd208401831058b410f to your computer and use it in GitHub Desktop.
Save koolquark/4ae66ad88e0a1cd208401831058b410f to your computer and use it in GitHub Desktop.
mongodb erlang skip and batchsize
2> application:ensure_all_started(mongodb).
{ok,[bson,poolboy,pbkdf2,mongodb]}
3>
3> Database = <<"test">>.
<<"test">>
4> {ok, Connection} = mc_worker_api:connect ([{database, Database},{w_mode, safe}]).
{ok,<0.181.0>}
5>
5> % Drop all the docs
5> Collection = <<"test">>.
<<"test">>
6> mc_worker_api:delete(Connection, Collection, {}).
{true,#{<<"n">> => 7}}
7>
7>
7> % Insert some docs
7>
7> Ret = mc_worker_api:insert(Connection, Collection, [
7> #{<<"name">> => <<"Yankees">>,
7> <<"home">> => #{<<"city">> => <<"New York">>, <<"state">> => <<"NY">>},
7> <<"league">> => <<"American">>},
7> #{<<"name">> => <<"Mets">>,
7> <<"home">> => #{<<"city">> => <<"New York">>, <<"state">> => <<"NY">>},
7> <<"league">> => <<"National">>},
7> #{<<"name">> => <<"Phillies">>,
7> <<"home">> => #{<<"city">> => <<"Philadelphia">>, <<"state">> => <<"PA">>},
7> <<"league">> => <<"National">>},
7> #{<<"name">> => <<"Red Sox">>,
7> <<"home">>=> #{<<"city">> => <<"Boston">>, <<"state">> => <<"MA">>},
7> <<"league">> => <<"American">>},
7> #{<<"name">> => <<"Red Sox1">>,
7> <<"home">>=> #{<<"city">> => <<"Boston">>, <<"state">> => <<"MA">>},
7> <<"league">> => <<"American">>},
7> #{<<"name">> => <<"Red Sox2">>,
7> <<"home">>=> #{<<"city">> => <<"Boston">>, <<"state">> => <<"MA">>},
7> <<"league">> => <<"American">>},
7> #{<<"name">> => <<"Red Sox3">>,
7> <<"home">>=> #{<<"city">> => <<"Boston">>, <<"state">> => <<"MA">>},
7> <<"league">> => <<"American">>}
7>
7> ]).
{{true,#{<<"n">> => 7}},
[#{<<"_id">> => {<<92,3,173,213,123,164,126,80,234,0,0,1>>},
<<"home">> =>
#{<<"city">> => <<"New York">>,<<"state">> => <<"NY">>},
<<"league">> => <<"American">>,<<"name">> => <<"Yankees">>},
#{<<"_id">> => {<<92,3,173,213,123,164,126,80,234,0,0,2>>},
<<"home">> =>
#{<<"city">> => <<"New York">>,<<"state">> => <<"NY">>},
<<"league">> => <<"National">>,<<"name">> => <<"Mets">>},
#{<<"_id">> => {<<92,3,173,213,123,164,126,80,234,0,0,3>>},
<<"home">> =>
#{<<"city">> => <<"Philadelphia">>,<<"state">> => <<"PA">>},
<<"league">> => <<"National">>,<<"name">> => <<"Phillies">>},
#{<<"_id">> => {<<92,3,173,213,123,164,126,80,234,0,0,4>>},
<<"home">> =>
#{<<"city">> => <<"Boston">>,<<"state">> => <<"MA">>},
<<"league">> => <<"American">>,<<"name">> => <<"Red Sox">>},
#{<<"_id">> => {<<92,3,173,213,123,164,126,80,234,0,0,5>>},
<<"home">> =>
#{<<"city">> => <<"Boston">>,<<"state">> => <<"MA">>},
<<"league">> => <<"American">>,<<"name">> => <<"Red Sox1">>},
#{<<"_id">> => {<<92,3,173,213,123,164,126,80,234,0,0,6>>},
<<"home">> =>
#{<<"city">> => <<"Boston">>,<<"state">> => <<"MA">>},
<<"league">> => <<"American">>,<<"name">> => <<"Red Sox2">>},
#{<<"_id">> => {<<92,3,173,213,123,164,126,80,234,0,0,7>>},
<<"home">> =>
#{<<"city">> => <<"Boston">>,<<"state">> => <<"MA">>},
<<"league">> => <<"American">>,
<<"name">> => <<"Red Sox3">>}]}
8>
8>
8> % skip 2 docs and set the size of each cursor step to 3
8> {ok, Cursor} = mc_worker_api:find(Connection, Collection, #{}, #{ skip => 2, batchsize => 3}).
{ok,<0.188.0>}
9>
9> mc_cursor:next_batch(Cursor).
[#{<<"_id">> => {<<92,3,173,213,123,164,126,80,234,0,0,3>>},
<<"home">> =>
#{<<"city">> => <<"Philadelphia">>,<<"state">> => <<"PA">>},
<<"league">> => <<"National">>,<<"name">> => <<"Phillies">>},
#{<<"_id">> => {<<92,3,173,213,123,164,126,80,234,0,0,4>>},
<<"home">> =>
#{<<"city">> => <<"Boston">>,<<"state">> => <<"MA">>},
<<"league">> => <<"American">>,<<"name">> => <<"Red Sox">>},
#{<<"_id">> => {<<92,3,173,213,123,164,126,80,234,0,0,5>>},
<<"home">> =>
#{<<"city">> => <<"Boston">>,<<"state">> => <<"MA">>},
<<"league">> => <<"American">>,
<<"name">> => <<"Red Sox1">>}]
10> mc_cursor:next_batch(Cursor).
[#{<<"_id">> => {<<92,3,173,213,123,164,126,80,234,0,0,6>>},
<<"home">> =>
#{<<"city">> => <<"Boston">>,<<"state">> => <<"MA">>},
<<"league">> => <<"American">>,<<"name">> => <<"Red Sox2">>},
#{<<"_id">> => {<<92,3,173,213,123,164,126,80,234,0,0,7>>},
<<"home">> =>
#{<<"city">> => <<"Boston">>,<<"state">> => <<"MA">>},
<<"league">> => <<"American">>,
<<"name">> => <<"Red Sox3">>}]
11> mc_cursor:next_batch(Cursor).
error
12>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment