in reply to Re: Need help with $slice MongoDB
in thread Need help with $slice MongoDB

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} })

Replies are listed 'Best First'.
Re^3: Need help with $slice MongoDB
by Mr. Muskrat (Canon) on Feb 04, 2014 at 16:09 UTC

    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 } );