can't get data from mongodb using perl (get undefined value) - mongodb

I am trying to get data from mongodb using perl, but I get undefined value for variable $people
my $client = MongoDB::MongoClient->new(host =>
'mongodb://xxx.xxx.xxx.xxx',port=>27017, username => 'xxxx',
password => 'xxxx');
my $db = $client->get_database("xxx");
my $collection = $db->get_collection("xxx");
my $people = $collection->find_one({"transactionid" => $id});
while (my $p = $people->next) {
print Dumper $p;
}
and I want to get this data :
{
"_id" : ObjectId("5c453500e2fb4adc98e9fa84"),
"transactionid" : NumberLong(45282),
"transactionbillerid" : NumberLong(43137),
"requesttime" : ISODate("2019-01-21T02:57:04.923Z"),
"requestmessage" : "xxxxxxxx",
"responsetime" : ISODate("2019-01-21T02:57:05.236Z"),
"responsemessage" : "xxx"
}
any suggestions, is there something wrong with my code ?

I think you're misunderstanding the value returned by find_one(). There's a big clue in the name, but find_one() returns a single record, not an iterator.
Obviously, I don't have access to your data, so I can't confirm this, but I expect you'll get what you want by running this code:
my $client = MongoDB::MongoClient->new(
host => 'mongodb://xxx.xxx.xxx.xxx',
port => 27017,
username => 'xxxx',
password => 'xxxx',
);
my $db = $client->get_database("xxx");
my $collection = $db->get_collection("xxx");
my $person = $collection->find_one({"transactionid" => $id});
print Dumper $person;

Related

Yii2-mongodb: How to catch mongodb insert returned mongo id

I would like to get the mongodb insert returned id for further insert specific operation my code is this:
$collection1 = $collection->insert(['username' => $this->email,
'password' =>sha1($this->input_password), 'full_name' => $this->full_name
'time_created' => '', 'time_updated' => '', 'time_last_visited' => '']);
when i does var_dump($collection1) function it shows me output
object(MongoId)#92 (1) { ["$id"]=> string(24) "54ba2b2c64363d640f000031" }
but when i try to access the id it shows me errors
i tried following options:
$insert_id = $collection1->primaryKey();
$insert_id = $collection1->getLastInsertID();
$id = $collection1->id;
$id = (string)$collection1['id'];
$id = $collection1['_id'];
but no options work. What is the right way?
Well i got my answer i tried another one differently & that works.
$id = (string) $collection1;
This returns me the string formatted mongoId.
$id = $collection->insert($data);

Unable to supply complex form values using WWW::Mechanize

I have an HTML form that I want to process with the WWW::Mechanize module. It seems that I can't POST an input value that contains a dollar sign $. I get this run time error message:
Can't call method "value" on an undefined value at script.pl line 599
My script looks like this
my $agent = WWW::Mechanize->new( autocheck => 1, ssl_opts => { verify_hostname => 0 }, );
my $formfiller = WWW::Mechanize::FormFiller->new();
$agent->env_proxy();
$agent->get('https://site.com/form.php');
$agent->form_number(1) if $agent->forms and scalar #{$agent->forms};
$agent->form_number(1);
{
local $^W;
$agent->current_form->value('page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_F40A1785D7B3490CBD5E72EDBE6B966D', 'John Doe');
}; #// First NAme
{
local $^W;
$agent->current_form->value('page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_E3316C46A4404C73ACBAE107DF6206D2', 'Bridgeport');
}; #// City
{
local $^W;
$agent->current_form->value('page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_E5747EB507E74DC1937557F9285CB57C', 'CT');
}; #// state
{
local $^W;
$agent->current_form->value('page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_A612569BE44C4BA3AD0AB3FBF8FB0553', '06604');
}; #// Zipcode
$agent->submit(Submit);
As far as I can see the problem is from the parameters to the value method, for instance
page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_A612569BE44C4BA3AD0AB3FBF8FB0553
How can I make Perl pass through the $, or to fix it in another way?
Are you sure you have?
$agent->submit(Submit);
Try this code instead:
$agent->get('https://site.com/form.php');
$agent->submit_form(
with_fields => {
'page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_F40A1785D7B3490CBD5E72EDBE6B966D' => 'John Doe',
'page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_E3316C46A4404C73ACBAE107DF6206D2' => 'Bridgeport',
'page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_E5747EB507E74DC1937557F9285CB57C' => 'CT',
'page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_A612569BE44C4BA3AD0AB3FBF8FB0553' => '06604'},
button => "Submit",);

MongoDB PHP Date

In my database I have a document like this.
{
"_id" : ObjectId("5465508f453c9446d228b225"),
"category" : "animals",
"lastLocation" : "Wilpattu",
"lastSeenDate" : "2013-05-26",
"company" : "53fd9a3204ac58132377f807"
}
I want to check if the given date is in between two date ranges. I am using Codeigniter for front end development. In the model class I have written,
$connection =$this->Dbconnect->GetMongoCon();
$database = $connection->etsp;
$collection = $database->trackedObjects;
$start = $AnimalArray['fyear']. '-'.$AnimalArray['fmonth'].'-'.$AnimalArray['fday'];
$end = $AnimalArray['lyear']. '-'.$AnimalArray['lmonth'].'-'.$AnimalArray['lday'];
$searchCriteria = array(
'company' => $AnimalArray["Company"],
'lastSeenDate' =>array('$gt' => $start1, '$lte' => $end1)
);
$ReturnAnimalArray=$collection->find($searchCriteria);
But I can't query the date. From internet I have tried converting date into mongodb format.
$start1 = new MongoDate(strtotime(date($start )));
$end1 = new MongoDate(strtotime(date($end)));
But that also did not work. Any hint will be highly appreciated.
lastSeenDate is not MongodbDate format, may be you should like do this:
$js = "function() {
return this.lastSeenDate > '2013-02-18' && this.lastSeenDate <= '2014-09-28';
}";
$collection->find(array('$where' => $js));

Tie Reading and Writing Perl Config Files

I'm using the PerlMonk example I found on:
Reading and Writing Perl Config Files
Configuration.pl:
%CFG = (
'servers' => {
'SRV1' => {
'IP' => 99.32.4.0,
'user' => 'aname',
'pswd' => 'p4ssw0rd',
'status' => 'unavailable'
},
'SRV2' => {
'IP' => 129.99.10.5
'user' => 'guest',
'pswd' => 'guest'
'status' => 'unavailable'
}
},
'timeout' => 60,
'log' => {
'file' => '/var/log/my_log.log',
'level' => 'warn',
},
'temp' => 'remove me'
);
It is working great, but the only issue is when reading and writing the HASH like configuration is being 'out of order'.
Is there a way to keep it TIED?
This important since the configuration file will be also edited manually, so I want the keys and values in the same order.
You could tie config variable before using it, so later hash keys will stay in same order as before,
use strict;
use warnings;
use Tie::IxHash;
tie my %CFG, 'Tie::IxHash';
%CFG = (
'servers' => {
'SRV1' => {
'IP' => '99.32.4.0',
'user' => 'aname',
'pswd' => 'p4ssw0rd',
'status' => 'unavailable'
},
'SRV2' => {
'IP' => '129.99.10.5',
'user' => 'guest',
'pswd' => 'guest',
'status' => 'unavailable'
}
},
'timeout' => 60,
'log' => {
'file' => '/var/log/my_log.log',
'level' => 'warn',
},
'temp' => 'remove me'
);
use Data::Dumper;
print Dumper \%CFG;
If you use JSON then you have the advantage that your software is safe from a malicious attack (or perhaps accidental corruption). JSON also has a simpler syntax than Perl data structures, and it is easier to recover from syntax errors.
Setting the canonical option will create the data with the keys in sorted order, and so generate the same output for the same Perl data every time. If you need the data in a specific order other than alphabetical then you can use the Tie::IxHash module as #mpapec describes in his answer.
Alternatively you can use the sort_by method from the Pure Perl version of the module that lets you pass a collation subroutine. That would let you prescribe the order of your keys, and could be as simple as using a hash that relates all the possible key values with a numerical sort order.
This program uses the sort_by method to reconstruct the JSON in the same order as the keys appear in your original hash. That is unlikely to be the order you want, but the mechanism is there. It works by looking up each key in a hash table to determine how they should be ordered. Any keys (like SVR1 and SVR2 here) that don't appear in the hash are sorted in alphabetical order by default.
use strict;
use warnings;
use JSON::PP ();
my %CFG = (
'servers' => {
'SRV1' => {
'IP' => '99.32.4.0',
'user' => 'aname',
'pswd' => 'p4ssw0rd',
'status' => 'unavailable'
},
'SRV2' => {
'IP' => '129.99.10.5',
'user' => 'guest',
'pswd' => 'guest',
'status' => 'unavailable'
}
},
'timeout' => 60,
'log' => {
'file' => '/var/log/my_log.log',
'level' => 'warn',
},
'temp' => 'remove me'
);
my %sort_order;
my $n = 0;
$sort_order{$_} = ++$n for qw/ servers timeout log temp /;
$sort_order{$_} = ++$n for qw/ IP user pswd status /;
$sort_order{$_} = ++$n for qw/ file level /;
my $json = JSON::PP->new->pretty->sort_by(\&json_sort);
print $json->encode(\%CFG);
sub json_sort {
my ($aa, $bb) = map $sort_order{$_}, $JSON::PP::a, $JSON::PP::b;
$aa and $bb and $aa <=> $bb or $JSON::PP::a cmp $JSON::PP::b;
}
generates this output
{
"servers" : {
"SRV1" : {
"IP" : "99.32.4.0",
"user" : "aname",
"pswd" : "p4ssw0rd",
"status" : "unavailable"
},
"SRV2" : {
"IP" : "129.99.10.5",
"user" : "guest",
"pswd" : "guest",
"status" : "unavailable"
}
},
"timeout" : 60,
"log" : {
"file" : "/var/log/my_log.log",
"level" : "warn"
},
"temp" : "remove me"
}
which can simply be saved to a file and similarly restored.

Grab the fsynclock status of a mongo db in perl

I'm trying to build a nagios check to check for how long a mongoDB has been locked using fsyncLock() for backup purposes (if the iSCSI snapshotting script blows up and the mongo is not being unlocked for example)
I was thinking about using a simple
$currentLock->run_command({currentOp => 1})
$isLocked = $currentLock->{fsyncLock}
But it seems like run_command() doesn't support currentOp yet. (As seen in there: https://github.com/MLstate/opalang/blob/master/lib/stdlib/apis/mongo/commands.opa)
Woudl anybody have an advice on how to check if a mongo is locked with a perl script? If not, I guess I'll go for some bash. I was thinking about using a db.eval('db.currentOp()') but I'm getting a bit lost.
Thanks!
You are right that run_command does not support doing a currentOp directly. However, if we look at the implementation of db.currentOp in the mongo shell, we can see how it works under the hood:
> db.currentOp
function (arg) {
var q = {};
if (arg) {
if (typeof arg == "object") {
Object.extend(q, arg);
} else if (arg) {
q.$all = true;
}
}
return this.$cmd.sys.inprog.findOne(q);
}
So we can query the special collection $cmd.sys.inprog on the Perl side to get the same inprog array that would be returned in the shell.
use strict;
use warnings;
use MongoDB;
my $db = MongoDB::MongoClient->new->get_database( 'test' );
my $current_op = $db->get_collection( '$cmd.sys.inprog' )->find_one;
When the server is not locked, it will return a structure in $current_op that looks something like this:
{
'inprog' => [
{
'connectionId' => 53,
'insert' => {},
'active' => bless( do{\(my $o = 0)}, 'boolean' ),
'lockStats' => {
'timeAcquiringMicros' => {
'w' => 1,
'r' => 0
},
'timeLockedMicros' => {
'w' => 9,
'r' => 0
}
},
'numYields' => 0,
'locks' => {
'^' => 'w',
'^test' => 'W'
},
'waitingForLock' => $VAR1->{'inprog'}[0]{'active'},
'ns' => 'test.fnoof',
'client' => '127.0.0.1:50186',
'threadId' => '0x105a81000',
'desc' => 'conn53',
'opid' => 7152352,
'op' => 'insert'
}
]
};
During an fsyncLock(), you'll get an empty inprog array but you will have a helpful info field and the expected fsyncLock boolean:
{
'info' => 'use db.fsyncUnlock() to terminate the fsync write/snapshot lock',
'fsyncLock' => bless( do{\(my $o = 1)}, 'boolean' ), # <--- that's true
'inprog' => []
};
So, putting it all together, we get:
use strict;
use warnings;
use MongoDB;
my $db = MongoDB::MongoClient->new->get_database( 'fnarf' );
my $current_op = $db->get_collection( '$cmd.sys.inprog' )->find_one;
if ( $current_op->{fsyncLock} ) {
print "fsync lock is currently ON\n";
} else {
print "fsync lock is currently OFF\n";
}
I actually decided to switch for a solution in bash (easier for what I want to do with the data later):
currentOp=`mongo --port $port --host $host --eval "printjson(db.currentOp())"`
Then some sort of grep -Po '"fsyncLock" : \d'
Thanks for the Perl insight though, it worked perfectly