vinaybond has asked for the wisdom of the Perl Monks concerning the following question:

Here is my sample json.
{ "_id" : ObjectId("52dfd2750cf2169251fe8d3c"), "closed_on" : ISODate("2014-01-30T18:30:00Z"), "companies" : [ { "_id" : ObjectId("52dfd2750cf2169251fe8d3f"), "comp_name" : "Test1", }, { "_id" : ObjectId("52dfd7950cf2169251fe8d47"), "comp_name" : "Test2", } ], "contacts" : [ { "_id" : ObjectId("52dfd2750cf2169251fe8d3e"), "fname" : "Solve", "lname" : "Scottt", "email" : "email1@gmail.com", "company" : "Test111", }, { "_id" : ObjectId("52dfd7950cf2169251fe8d46"), "fname" : "signe", "lname" : "towne", "email" : "email2@gmail.com", "company" : "Test222", } ], "crt_by" : "creator@gmail.com", "currency" : "USD", "type" : "None", } I need: crt_by conatacts.fname ( from the first record of the contacts array) conatacts.company ( from the first record of the contacts array) I tried using $slice like this. my $output = $collection->find({} },{ crt_by => 1, 'contacts' => {'$s +lice' => 1} }); and when I am printing the the record it shows me some array for conta +cts. while (my $doc = $output->next) { $output.= "\t- ".$doc->{'crt_by'}." Contact".$doc->{'c +ontacts.fname'}."\n"; } } print "$ouput\n";
What is the right command to do so. -Vinay

Replies are listed 'Best First'.
Re: Need help with $slice MongoDB
by Mr. Muskrat (Canon) on Jan 28, 2014 at 19:40 UTC

    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.

      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 } );
Re: Need help with $slice MongoDB
by simmisam (Novice) on Jan 30, 2014 at 03:21 UTC

    Check the below code. I used the JSON module, but it gave me error on decoding the string provided by you. Instead of ID value ObjectId("52dfd2750cf2169251fe8d3c"), it's "ObjectId(52dfd2750cf2169251fe8d3c)" that worked with JSON module. I don't know whether you have mentioned the correct json string or not.

    use JSON qw(decode_json); my $jsonString = '{ "_id" : "ObjectId(52dfd2750cf2169251fe8d3c)", "closed_on": "ISODate(2014-01-30T18:30:00Z)", "companies": [ { "_id": "ObjectId(52dfd2750cf2169251fe8d3f)", "comp_name": "Test1" }, { "_id": "ObjectId(52dfd7950cf2169251fe8d47)", "comp_name": "Test2" } ], "contacts" : [ { "_id": "ObjectId(52dfd2750cf2169251fe8d3e)", "fname": "Solve", "lname": "Scottt", "email": "email1@gmail.com", "company": "Test111" }, { "_id": "ObjectId(52dfd7950cf2169251fe8d46)", "fname": "signe", "lname": "towne", "email": "email2@gmail.com", "company": "Test222" } ], "crt_by": "creator@gmail.com", "currency": "USD", "type": "None" }'; my $decodedJSON = decode_json($jsonString); print "crts_by = $decodedJSON->{'crt_by'}\n"; print "Contact 1 => fname = $decodedJSON->{'contacts'}[0]{'fname'}, co +mpany = $decodedJSON->{'contacts'}[0]{'company'}\n";