Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.
Showing posts with label mongodb. Show all posts
Showing posts with label mongodb. Show all posts

2015/11/06

Trying to Read MongoDB dump files using BSON

I've been looking at increasing the amount of MongoDB we use at work, and this includes backing up the data. Due to my own confusion, I had a little issue getting mongodump to work, but I have been able to dump from one Mongo database and restore to another.

mongodump writes in a format called Binary JSON, or BSON. I installed BSON.pm with the intention of reading the BSON file and ensuring it works. with small tests, I was able to put objects into BSON, write to file, read from file, and use Data::Dumper to display the object is what I wanted it to be.

But, I find I cannot read the file, because BSON.pm reports it as having an incorrect length.


I fully expect that I'm doing something subtly stupid, but what it could be isn't immediately obvious. the contents of $bson should be exactly as written to file, and mongorestore was happy with it. encode() and decode() had worked acceptably, but admittedly on a much smaller dataset than the one I'm working with here, which contains several months of status updates.

I suppose I don't need to read the BSON files, but I do like being able to check the contents of a database before restoring.

2015/05/13

Being Open, Being Wrong, Being Corrected, Being Smarter: Response about MongoDB

Just heard a podcast from Developer Tea where Ben Lesh of Netflix explain how he likes it when he makes mistakes, because then he learns, and we should always be learning. (I listened to it when driving, so I didn't write down the quote, and I'm not sure if it's in part 1 or part 2 of the interview.)

Yesterday, I posted on issues I had with MongoDB and Perl. I had done what I had wanted before, I was sure, but my code no longer reflected it, and Shub-Internet was not responding with the answers I desired. If you don't recall:

# This worked to get one value:
my $find_one = $collection->find_one( { date => $today }) ;

# but if I wanted the whole thing, this didn't work:
my $find = $collection->find ;

As it turns out, there's a specific piece of information I did not find, but David Golden of MongoDB set me straight.

# but if I wanted the whole thing, this didn't work:
my $find = $collection->find ;
my @all = $find->all ;
# and in the case I'd want:
my %all =
    map { $_->{ date } => $_ } #map, grep and sort are all-powerful
    $find->all ;
say Dumper \%all ;

This is great. Wonderful, even. I had a failure in understanding, I was open about it, I was corrected, and now I am a better programmer for it.

I do sorta wish I had given Stack Overflow a deeper search first, though....

2015/05/12

Having an issue with MongoDB and Perl

I've learned to love SQL. At first I had lots of functions that did SELECT * FROM table, then I would put everything together at the last moment, with big slow programs. Slowly but surely, I found myself trusting my tools more and more, writing more and more complex SQL queries to give me exactly what I needed, in as close to the right form as I could.

Now that I have some level of expertise with SQL, we have it as a core of our infrastructure at work, so any experimenting with NoSQL databases. It's mostly for my toy bits and administrivia, not really holding any real data, but I am keeping my current FitBit step count in it.

I am currently finding that I can find_one() all I want, but I can't find(). I am sure I used to. I'm also sure that I'm doing something stupid, not the MongoDB Perl module, but I can't tell what I'm doing wrong.

Help?


2014/01/31

Trying just MongoDB, and it works!

And it works!

#!/usr/bin/perl

use feature qw{ say state } ;
use strict ;
use warnings ;
use Data::Dumper ;
use MongoDBx::Class ;
use MongoDB ;

my @movies ;
push @movies,
    {
    title           => 'Blade Runner',
    rating          => 'R',
    releaseYear     => '1982',
    hasCreditCookie => 1
    } ;
push @movies,
    {
    title           => 'Thor',
    rating          => 'PG-13',
    releaseYear     => '2011',
    hasCreditCookie => 1
    } ;

{
    my $client     = MongoDB::MongoClient->new( host => 'localhost', port => 27017 ) ;
    my $database   = $client->get_database( 'test' ) ;
    my $collection = $database->get_collection( 'movies' ) ;
    my $movies     = $collection->find( ) ; # READ
    while ( my $movie = $movies->next ) { # would prefer for ( @$movies ) {} but oh well
        my $title = $movie->{ title } ? $movie->{ title } : 'none' ;
        my $releaseYear = $movie->{ releaseYear } ? $movie->{ releaseYear } : 'none' ;
        my $count = $movie->{ count } ? $movie->{ count } : 0 ;
        say qq{$title ($releaseYear) - $count } ;
        my $id = $movie->{ _id } ; # every mongodb record gets an _id
        $collection->remove( { _id => $id } ) if $title eq 'none' ; # DELETE
        $collection->update(
            { title => $title },
            { '$set' => { count => 1 + $count } }
            ) ; # UPDATE
        }
    #LADIES AND GENTLEMEN, WE HAVE CRUD!!!
    }
exit ;
{
    my $client     = MongoDB::MongoClient->new( host => 'localhost', port => 27017 ) ;
    my $database   = $client->get_database( 'test' ) ;
    my $collection = $database->get_collection( 'movies' ) ;
    my $data       = $collection->find( ) ;
    for my $movie ( @movies ) {
        say qq{$movie->{ title } ($movie->{ releaseYear })} ;
        $collection->insert( $movie ) ; # CREATE
        }
    }

The lesson might be Don't Trust ORMs, Do It Yourself, but I hope that there's some reason to use these things.