diff --git a/lib/Catmandu/Store/MongoDB/Bag.pm b/lib/Catmandu/Store/MongoDB/Bag.pm index 80621bc..b9d5184 100644 --- a/lib/Catmandu/Store/MongoDB/Bag.pm +++ b/lib/Catmandu/Store/MongoDB/Bag.pm @@ -175,6 +175,29 @@ sub pluck { ); } +sub slice { + my($self, $start, $limit) = @_; + + $start //= 0; + + Catmandu::Iterator->new( + sub { + sub { + # limit 0 == all in mongodb + return if defined($limit) && $limit <= 0; + + state $cursor = do { + my $c = $self->_cursor({}); + $c->skip($start); + $c->limit($limit) if defined $limit; + $c; + }; + $cursor->next; + } + } + ); +} + sub get { my ($self, $id) = @_; $self->collection->find_one({_id => $id}, {}, $self->_options); diff --git a/t/01-store.t b/t/01-store.t index 1ccbd1a..22589d7 100644 --- a/t/01-store.t +++ b/t/01-store.t @@ -66,6 +66,10 @@ is_deeply $store->bag->searcher( is_deeply $store->bag->search(query => {char => "ABC"}, fields => {_id => 1}) ->first, {_id => '789'}; +is_deeply $store->bag->slice(0)->to_array(), [{_id => '789', char => 'ABC', num => '123'}]; +is_deeply $store->bag->slice(1)->to_array(), []; +is_deeply $store->bag->slice(0,0)->to_array(), []; + END { if ($db) { $db->drop;