Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2014/01/31

You Had One Job, MongoDBx::Class Documentation

So, after yesterday, I started back with the command line interface to MongoDB. I found I can do what I want with it, which of course is creating, retrieving, updating and deleting records. So, I try working with Perl, my go-to language, and I install MongoDBx::Class. The following code, especially the $db-&gtlinsert() is closely adapted from the CPAN page.


  1. #!/usr/bin/perl  
  2.   
  3. use feature qw{ say state } ;  
  4. use strict ;  
  5. use warnings ;  
  6. use MongoDBx::Class ;  
  7.   
  8. my $dbx = MongoDBx::Class->new( namespace => 'MyApp::Model::DB' ) ;  
  9. my $conn = $dbx->connect(host => 'localhost', port => 27017 , safe => 1 );  
  10. my $db = $conn->get_database( 'test' ) ;  
  11.   
  12. my @movies ;  
  13. push @movies , {  
  14.     title => 'Blade Runner' ,  
  15.     rating => 'R' ,  
  16.     releaseYear => '1982' ,  
  17.     hasCreditCookie => 1  
  18.     } ;  
  19. push @movies , {  
  20.     title => 'Thor' ,  
  21.     rating => 'PG-13' ,  
  22.     releaseYear => '2011' ,  
  23.     hasCreditCookie => 1  
  24.     } ;  
  25.   
  26. for my $movie ( @movies ) {  
  27.     say qq{$movie->{ title } ($movie->{ releaseYear })};  
  28.     $db->insert($movie) ; # How the documentation says it should go  
  29.     }  


No comments:

Post a Comment