Perl and MongoDB update issue - perl

i tried to use https://metacpan.org/release/MongoDB module over perl and found a small bug or i haven't enough material on doc
this is where i got error
{ "_id" : ObjectId("4f625c0fcd4481bc13000000"), "mode" : "running", "res" : "running", "custid":NumberLong(155655062)}
db.movie.update({custid:NumberLong(155655062)},{'$set':{mode:"testing"}});
I am able to update the thing here but not able to update from perl code.
my $res = $db->movie->update({'custid'=>'NumberLong(155655062)'},{'$set' => { 'mode' => 'testing' }});
did i miss any code in the above example

my $cust_id = 155655062; OR my $cust_id = int($record->{custid});
my $res = $db->movie->update({'custid'=>$cust_id},{'$set' => { 'mode' => 'testing' }});
Try this, it should work.

Related

How do you get Posts by multiple meta_keys and meta_values with the Rest API V2?

I'm trying to get my Posts by multiple meta_keys and meta_values. How do I accomplish this? The URL should be looking like this:
"/posts?meta_key=Example&meta_value=Example2&meta_key=Example3&meta_value=Example4"
I tried to find a solution for this quite a while now, but couldn't find anything as most things were outdated.
I found a solution for my Problem. What i did is quite simple. I just setted up a new Query Params.
if (!function_exists('post_meta_rest_api_request')) :
function post_meta_rest_api_request($argu, $request)
{
$argu += array(
'meta_key' => $request['meta_key'],
'meta_value' => $request['meta_value'],
'meta_query' => $request['meta_query'] == 1 ? array(
array(
"key" => "key1",
"value" => "value1"
),
array(
"key" => "key2",
"value" => "value2"
)
) : $request['meta_query']
);
return $argu;
}
add_filter('rest_custom_query', 'post_meta_rest_api_request', 99, 2);
endif;
So if you now make an API call like: wordpress/wp-json/wp/v2/customType?meta_query=1
the API Request will take in your custom Query Params. Otherwise it will just take in the normal Meta Query Request.
For references look here: WordPress Rest-API Requests

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

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;

group by email address is not working with mongodb in php

I want to do group by with email_address field in mongodb in php, here is my code for this but it is not working, can anyone please look into that and give me proper solution for that ?
$m = new MongoDB\Client("mongodb://localhost:27017");
$db = $m->mailchimp;
$collection = $db->users;
$aggregate[] = [
'$group' => [
'_id' => 'email_address',
'email_address' => array('$sum'=>'1'),
],
];
$data=$collection->aggregate($aggregate);
I think you are missing $ in query.
'_id' => '$email_address',
'$email_address' => array('$sum'=>'1')
Ref
PHP Syntax

Why $pull operator doesn't work on MongoDB when using empty criteria?

I have nodes like this:
[_id] => MongoId Object (
[$id] => 4e90cb3cd68417740c000017
)
[label] => mystery
[owner] => me
[parents] => Array (
[0] => Array (
[id] => MongoId Object (
[$id] => 4e8c6bb6d68417340e0004ca
)
[owner] => userid
[timestamp] => 1318112522
)
)
[timestamp] => 1318112060
[translabel] => mystery
[type] => 0
What I am trying to do is to remove parents with id : 4e8c6bb6d68417340e0004ca , wherever they are.
For example this should have been working (latest Mongo):
db.nodes.update({},{$pull : {"parents": { "id" : ObjectId("4e8c6bb6d68417340e0004ca") }}});
or equaly in PHP (latest driver etc):
$mongodb->nodes->update(array(),array('$pull'=> array('parents'=>array('id'=> new MongoId("4e8c6bb6d68417340e0004ca")))));
both don't do anything!
on the other hand:
db.nodes.update({"_id": ObjectId("4e90cb3cd68417740c000017")},{$pull : {"parents": { "id" : ObjectId("4e8c6bb6d68417340e0004ca") }}});
or equaly in PHP:
$mongodb->nodes->update(array('_id' => new MongoId("4e90cb3cd68417740c000017"),array('$pull'=> array('parents'=>array('id'=> new MongoId("4e8c6bb6d68417340e0004ca")))));
work perfectly well! Is this a bug? Is it a problem that I use "id" instead of "_id" with MongoID objects in my subdocuments? Thanks in advance for any help!
Have you tried enable multi flag on update command
db.collection.update( criteria, objNew, upsert, multi )
multi - indicates if all documents matching criteria should be updated rather than just one. Can be useful with the $ operators below.
and change your query to
db.nodes.update({},{$pull : {"parents": { "id" : ObjectId("4e8c6bb6d68417340e0004ca") }}},false,true);
or
db.nodes.update({ "_id" : { $exists : true } },{$pull : {"parents": { "id" : ObjectId("4e8c6bb6d68417340e0004ca") }}},false,true);
i haven't tested the code myself, but am sure that any one of the above will work..

Why am I getting a "Odd number of elements in anonymous hash" warning in Perl?

Help, I'm trying to create a new post in my wordpress blog with custom fields using the following perl script using metaweblogAPI over XMLRPC, but there seems to be an issue with the custom fields. Only the second custom field (width) ever seems to get posted. Can't get the "height" to publish properly. When I add another field, I get the "Odd number of elements in anonymous hash" error. This has got to be something simple - would someone kindly sanity check my syntax? Thanks.
#!/usr/bin/perl -w
use strict;
use RPC::XML::Client;
use Data::Dumper;
my $cli=RPC::XML::Client->new('http://www.sitename.com/wp/xmlrpc.php');
my $appkey="perl"; # doesn't matter
my $blogid=1; # doesn't matter (except blogfarm)
my $username="Jim";
my $passwd='_____';
my $text=<<'END';
This is the post content...
You can also include html tags...
See you!
END
my $publish=0; # set to 1 to publish, 0 to put post in drafts
my $resp=$cli->send_request('metaWeblog.newPost',
$blogid,
$username,
$passwd,
{
'title' => "this is doodoo",
'description' => $text,
'custom_fields' => {
{ "key" => "height", "value" => 500 },
{ "key" => "width", "value" => 750 }
},
},
$publish);
exit 0;
While techically valid syntax, it's not doing what you think.
'custom_fields' => {
{ "key" => "height", "value" => 500 },
{ "key" => "width", "value" => 750 }
},
is roughly equivalent to something like:
'custom_fields' => {
'HASH(0x881a168)' => { "key" => "width", "value" => 750 }
},
which is certainly not what you want. (The 0x881a168 part will vary; it's actually the address where the hashref is stored.)
I'm not sure what the correct syntax for custom fields is. You can try
'custom_fields' => [
{ "key" => "height", "value" => 500 },
{ "key" => "width", "value" => 750 }
],
which will set custom_fields to an array of hashes. But that may not be right. It depends on what send_request expects.