in reply to Need help with $slice MongoDB

I don't use MongoDB much and you didn't provide a working script to start with so I can't really provide an answer. I can however point you to a snippet of code that I found that might help. I did a Google search for "perl mongodb slice" without the quotes and the fifth result was Example of using $slice and fields() in the MongoDB Perl driver 0.45.

Replies are listed 'Best First'.
Re^2: Need help with $slice MongoDB
by vinaybond (Novice) on Feb 04, 2014 at 07:35 UTC
    Hi, Thanks for the reference. I have been through the link. Here is the code using which I am retrieving the records. It give me crt_by, name, and all the field from first array. Instead of Dumper I am using YAMP and parsing each row to get the data.
    my $some_users = $collection->find({},{ crt_by => 1, name => 1, 'cont +acts' => {'$slice' => 1} })

      Now that I'm looking at the problem again, I don't see that usage of find in the MongoDB::Collection docs. Are you sure that you don't want to use find with fields (and then either a slice or limit of the resultant cursor, possibly with a sort_by as well) or find_one instead?

      What I have put together here is untested and should be considered pseudo code but it seems to me that one of the following would do the trick.

      my $some_users = $collection->find->fields( { crt_by => 1, name => 1, + contacts => { '$slice' => 1 } } ); #my $some_users = $collection->find->limit( 1 )->fields( { crt_by => 1 +, name => 1, contacts => 1 } ); #my $some_users = $collection->find_one( {}, { crt_by => 1, name => 1 +, contacts => 1 } );