Advertisement
hackloper775

mongo_db_perl

Jul 20th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.52 KB | None | 0 0
  1. #!/usr/bin/env perl # <-Hashbang
  2.  
  3. use MongoDB; # <- Modulo
  4. use MongoDB::Database;
  5. use strict;
  6.  
  7. use warnings; # <- Para desarrollo
  8.  
  9. # Definimos algunas variable para conectarnos a la Base de Datos
  10. # Como el nombre de la base de datos
  11. # El puerto,etc
  12.  
  13. my $database = "test";
  14.  
  15. my $puerto = 27017;
  16.  
  17. my $host = "localhost";
  18.  
  19. # Iniciamos la conexion
  20.  
  21. $host = sprintf("%s:%s",$host,$puerto);
  22.  
  23. my $cliente = MongoDB::MongoClient->new("host" => $host );
  24.  
  25. # Nos conectamos a la base de datos
  26.  
  27. my $base_datos  = $cliente->get_database( $database );
  28.  
  29. my @collections = $base_datos->collection_names;
  30.  
  31. for (my $i = 0; $i < scalar(@collections); $i++)
  32. {
  33.     print $i,":",$collections[$i],"\n";
  34. }
  35.  
  36. my $coll = {};
  37.  
  38. print "Elige una base de datos : ";
  39.  
  40. my $R_c = <STDIN>;
  41.  
  42. chop($R_c);
  43.  
  44. $coll->{$collections[$R_c]} = $base_datos->get_collection($collections[$R_c]);
  45.  
  46. my $object = $coll->{$collections[$R_c]}->find;
  47.  
  48.  
  49. =pod Comentarios
  50.  
  51. Aqui abajo podemos llamar las funciones
  52.  
  53. =cut
  54.  
  55.  
  56. sub insertar
  57. {
  58.     $coll->{$collections[$R_c]}->insert({
  59.         "_id" => 2,
  60.         "value" => "Hola Mundo desde Perl y MongoDB" # Esto depende de que queramos insertar
  61.     });
  62. }
  63.  
  64. sub ver_uno
  65. {
  66.     foreach my $keys(keys $object)
  67.     {
  68.        print $object->{$keys},"\n";
  69.     }
  70. }
  71.  
  72. sub ver_todo
  73. {
  74.     my $all = $coll->{$collections[$R_c]}->find;
  75.     while (my $doc = $all->next)
  76.     {
  77.         foreach my $keys (keys $doc)
  78.         {
  79.             print $doc->{$keys}," ";
  80.         }
  81.         print "\n";
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement