Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env perl # <-Hashbang
- use MongoDB; # <- Modulo
- use MongoDB::Database;
- use strict;
- use warnings; # <- Para desarrollo
- # Definimos algunas variable para conectarnos a la Base de Datos
- # Como el nombre de la base de datos
- # El puerto,etc
- my $database = "test";
- my $puerto = 27017;
- my $host = "localhost";
- # Iniciamos la conexion
- $host = sprintf("%s:%s",$host,$puerto);
- my $cliente = MongoDB::MongoClient->new("host" => $host );
- # Nos conectamos a la base de datos
- my $base_datos = $cliente->get_database( $database );
- my @collections = $base_datos->collection_names;
- for (my $i = 0; $i < scalar(@collections); $i++)
- {
- print $i,":",$collections[$i],"\n";
- }
- my $coll = {};
- print "Elige una base de datos : ";
- my $R_c = <STDIN>;
- chop($R_c);
- $coll->{$collections[$R_c]} = $base_datos->get_collection($collections[$R_c]);
- my $object = $coll->{$collections[$R_c]}->find;
- =pod Comentarios
- Aqui abajo podemos llamar las funciones
- =cut
- sub insertar
- {
- $coll->{$collections[$R_c]}->insert({
- "_id" => 2,
- "value" => "Hola Mundo desde Perl y MongoDB" # Esto depende de que queramos insertar
- });
- }
- sub ver_uno
- {
- foreach my $keys(keys $object)
- {
- print $object->{$keys},"\n";
- }
- }
- sub ver_todo
- {
- my $all = $coll->{$collections[$R_c]}->find;
- while (my $doc = $all->next)
- {
- foreach my $keys (keys $doc)
- {
- print $doc->{$keys}," ";
- }
- print "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement