Cookie Notice

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

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?


#!/usr/bin/env perl
use feature qw'say' ;
use strict ;
use warnings ;
use DateTime ;
use Data::Dumper ;
use lib '/home/jacoby/lib' ;
use MongoConnect ; #handles access to mongo database so passwords need not show up in gists
$MongoConnect::Database = 'local' ; # which of all possible Mongo DBs should I connect to?
my $today = DateTime->now()->set_time_zone( 'America/New_York' )->ymd ; # 2015-05-12
say $today ;
my $client = get_client() ; #think "MongoDb::MongoClient->new()"
my $database = $client->get_database( 'fitbit' ) ;
my $collection = $database->get_collection( 'daily_reads' ) ;
my $find = $collection->find ;
say Dumper $find ;
$find = $collection->find_one( { date => $today }) ;
say Dumper $find ;
view raw mfitbit.pl hosted with ❤ by GitHub
2015-05-12
$VAR1 = bless( {
'_request_id' => 0,
'_client' => bless( {
'timeout' => 20000,
'auto_connect' => 1,
'db_name' => 'admin',
'auto_reconnect' => 1,
'_servers' => {},
'ts' => 0,
'max_wire_version' => 3,
'w' => 1,
'find_master' => 0,
'_max_write_batch_size' => 1000,
'_readpref_retries' => 3,
'host' => 'localhost',
'inflate_regexps' => 0,
'port' => '27017',
'dt_type' => 'DateTime',
'inflate_dbrefs' => 1,
'ssl' => 0,
'_opts' => {
'port' => '27017',
'host' => 'localhost'
},
'_max_bson_wire_size' => 16793600,
'j' => 0,
'query_timeout' => 30000,
'max_bson_size' => 16777216,
'_readpref_pingfreq_sec' => 5,
'_readpref_mode' => 0,
'wtimeout' => 1000,
'_is_mongos' => 0,
'sasl_mechanism' => 'GSSAPI',
'min_wire_version' => 0,
'sasl' => 0
}, 'MongoDB::MongoClient' ),
'_batch_size' => 0,
'immortal' => 0,
'_limit' => 0,
'_master' => $VAR1->{'_client'},
'_agg_batch_size' => 0,
'_is_parallel' => 0,
'slave_okay' => 0,
'_skip' => 0,
'started_iterating' => 0,
'partial' => 0,
'_tailable' => 0,
'_query' => bless( [
{},
[],
[],
0
], 'Tie::IxHash' ),
'_ns' => 'fitbit.daily_reads'
}, 'MongoDB::Cursor' );
$VAR1 = {
'date' => '2015-05-12',
'_id' => bless( {
'value' => '5551f9552dcb56497f7bac33'
}, 'MongoDB::OID' ),
'time' => '2015-05-12T16:00:04.306971',
'steps' => 12376
};
via the Mongo shell
> db.daily_reads.find()
{ "_id" : ObjectId("5551f9552dcb56497f7bac33"), "date" : "2015-05-12", "steps" : 12376, "time" : "2015-05-12T16:00:04.306971" }
> show dbs
administration (empty)
dave 0.0625GB
fitbit 0.0625GB
local (empty)
test (empty)
> use fitbit
switched to db fitbit
> show collections
daily_reads
system.indexes
> db.daily_reads.find()
{ "_id" : ObjectId("5551f9552dcb56497f7bac33"), "date" : "2015-05-12", "steps" : 12376, "time" : "2015-05-12T16:00:04.306971" }
>

No comments:

Post a Comment