Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re^7: MongoDB replacing an array

by Mr. Muskrat (Canon)
on Aug 26, 2012 at 23:38 UTC ( #989891=note: print w/replies, xml ) Need Help??

in reply to Re^6: MongoDB replacing an array
in thread MongoDB replacing an array

I'm hoping that I can learn a bit about MongoDB from trying to help you. Is the update happening within the @cols foreach loop? How are you getting $col? $col = $db->$cols;?

Based on your @new foreach print, it looks like you are storing multiple timestamps in each record. Is that what you intended? If so, it seems odd to me to store timestamps in a column named timestamp. :)

Replies are listed 'Best First'.
Re^8: MongoDB replacing an array
by neilwatson (Priest) on Aug 27, 2012 at 00:21 UTC

    Yes, I am getting $col as you describe. No issue there. Yes, multiple timestamps in a record. This is what I intend.

    Neil Watson

      Perhaps my short example script that seems to be working will help shed some light on what's wrong with yours? At the very least, I've gotten a little exposure to MongoDB out of this experience.

      #!/bin/env perl use strict; use warnings; use Data::Dumper; use MongoDB; use Try::Tiny; my $conn; # the connection can fail try { $conn = MongoDB::Connection->new; } catch { chomp( my $err = $_ ); $err =~ s/ at .*//; die "Unable to connect to the default MongoDB ($err)"; }; my $db = $conn->mydb; # shouldn't ever die because MongoDB will create + it if it doesn't exist my $hosts = $db->hosts; # ditto $hosts->insert({host => "", timestamp => [ qw( 20110101 20110 +202 20110303 ) ]}); $hosts->insert({host => "", timestamp => [ qw( 20110101 20110 +202 20110303 ) ]}); $hosts->insert({host => "", timestamp => [ qw( 20110707 20110 +808 20110909 ) ]}); $hosts->insert({host => "", timestamp => [ qw( 20111010 20111 +111 20111212 ) ]}); $hosts->insert({host => "", timestamp => [ qw( 20110404 20110 +505 20110606 ) ]}); my $all_hosts = $hosts->find; my $ts; while ( my $host = $all_hosts->next ) { push @{$ts->{$host->{host}}}, @{$host->{timestamp}}; } # add non-existant host just to try and get an exception $ts->{''} = [ qw( 20120606 ) ]; $all_hosts->reset; for my $host ( keys %$ts ) { print "Updating $host..."; my @new = @{ $ts->{$host} }; # I wasn't able to get it to die even when trying to update a non- +existant record # $rc->{updatedExisting} will contain a true/false value but $rc-> +{err} is always undef # but I feel better trying to catch the croak my $rc; try { $rc = $hosts->update( { host => $host }, { '$set' => { timestamp => [ @new ] } }, { multiple => 1, safe => 1 } ); } catch { warn sprintf( "Couldn't update records for host %s, %s", $host +, $_ ); }; print $rc->{updatedExisting} ? 'success' : 'failure', "\n"; } while ( my $host = $all_hosts->next ) { printf "%s => %s\n", $host->{host}, Dumper( $host->{timestamp} ); } $hosts->drop; $db->drop;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://989891]
[Corion]: Maybe you could reduce ITIL to a computer network, a man and a dog. The network runs the computations. The man is there to feed the dog. The dog is there to prevent the man from accessing the computers.
[marto]: from experience of this side of the fence, it's all about proving to people that you are ITIL compliant, then the client ignores logical processes anyway :P
[marto]: when you work on the outside, for a client, you can make them aware of the stupids, but they don' t always listen :/
[Corion]: Yeah, you can lead them to water, but it's illegal to drown them...
[Discipulus]: Here my solution marto: create a win fake machine insied AD. do a micro fake partition foreach remote partition you wont to monit. put a web server that acept from data from remote systems and full fake partition as needed. fake partition are Mb. ;=)
[marto]: Discipulus yeah, the nuclear industry love fake machines sitting around on a controlled domain :P
[marto]: changing a typo on a website is often a great deal of paper work :P

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2017-01-24 10:35 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (203 votes). Check out past polls.