Perl mongo->collection count using '$in' operator - perl

I was wondering if it was possible to use the "in" operator as you can from the mongo shell, using the perl MongoDB::Collection module. I have tried a number of things, but haven't quite got the result I am expecting. I've check the docs and other posts on stackoverflow but can't seem to find anything specifically about this, unless I am overlooking something.
http://docs.mongodb.org/manual/reference/operator/query/in/
The count query I am running via the mongo shell is
mongo:PRIMARY> db.getCollection("Results").count( { TestClass : "TestClass", TestMethod : { $in: ["method1" , "method2", "method3"] } })
181605
I have tried this a few different ways passing the list as an array or hash-refs or pre-building a string...
my $count = $mongo->{collection}->count({
'TimeStamp' => { '$gt' => $ft, '$lt' => $tt },
'TestClass' => $TestClass,
'TestMethod' => { '$in' => [$whitelist->methods] },
'Result' => $result
});
Where Dumping $whitelist->methods is
$VAR1 = {
'method1' => 1,
'method2' => 1,
'method3' => 1
};
I've looked high and low for an answer, does anyone know if the driver is currently capable of using the $in operator like this? Looping through the returned methods from a previous query and adding up the results will require more code.
The only other stack overflow post I have seen about the $in operator was this $in mongoDB operator with _id in perl recommending using http://api.mongodb.org/perl/current/MongoDB/OID.html but don't think that is relevant in my example as looks more to do with ID's.
Any help or discussion would be greatly appreciated.

The problem is that $in clause expects its value to be an array reference, but you supply a hashref (as Dumper's output shows) into it. The easiest way to turn the latter into the former is to apply keys function:
# ...
'TestMethod' => { '$in' => [keys %{$whitelist->methods}] }
... or just [keys $whitelist->methods], if you're using Perl 5.14+, as ...
starting with Perl 5.14, keys can take a scalar EXPR, which must
contain a reference to an unblessed hash or array
.

Related

Mongodb: Is it possible to run map-reduce command and query strings as integers

So I have the following PHP code which runs a map-reduce command on a MongoDB database collection:
$map = new MongoCode("function() { emit(this.app, this.bytes); }");
$reduce = new MongoCode("function(k, vals) { ".
"var sum = 0;".
"for (var i in vals) {".
"sum += vals[i];".
"}".
"return sum; }");
$dateAdded = mktime(0,0,0,5,1,2015);
//echo $dateAdded." = ".date("r",$dateAdded)."<br>\n";
$request = $db->command(array(
"mapreduce" => "log",
"map" => $map,
"reduce" => $reduce,
"query" => array("event" => "destroy", "systimelong" => array('$gt' => $dateAdded)),
"out" => array("inline" => 1)));
var_dump($request);
This actually works really great when the data is stored in the database as an integer. But sometimes the data gets stored as a string. Why? Thats another story that cant be changed right now. Ultimately it will be an integer, but I'd really like to know if and how I can modify this to handle the cases that the data is a string just in case it ever happens.
Since Mongo uses Javscript I feel like I should be able to use the parseInt() function inside the $map and/or$reduce functions, but it doesn't seem to be working.
Also, how would I handle the query? The systimelong field is just unixtime, and I am using the PHP mktime() function to generate a integer value for the beginning of the month. Again, it works great then comparing integers, but I need to first convert the string value.
Any ideas?

Perl module for Elastisearch Percolator

I'm trying to use the Elasticsearch Percolator with perl and I have found this cool module.
The Percolation methods are listed here
As far as I can tell they're just read methods, hence it is only possible to read the queries index and see if a query already exists, count the queries matched, etc.
Unless I'm missing something it is not possible to add queries via the Percolator interface, so what I did is use the normal method to create a document against the .percolator index as follow:
my $e = Search::Elasticsearch->new( nodes => 'localhost:9200' );
$e->create(
index => 'my_index',
type => '.percolator',
id => $max_idx,
body => {
query => {
match => {
...whatever the query is....
},
},
},
);
Is that the best way of adding a query to the percolator index via the perl module ?
Thanks!
As per DrTech answer the code I posted looks to be the correct way of doing it.

$in mongoDB operator with _id in perl

I try to execute this query with perl on a MongoDB database :
$db->$collection->find({"_id" : { "$in" : ["4f520122ecf6171327000137", "4f4f49c09d1bd90728000034"]}});
But it return nothing and it must return two documents. What is wrong with this query ?
Thank you.
Edit : It doesn't work too :
$db->$collection->find( {_id => "4f520122ecf6171327000137"} );
First, make sure you're using the correct syntax. Your first example is not valid Perl code, since you're including a chunk of JSON as the query parameter.
Second, assuming these ID values are MongoDB ObjectID's, you'll need to make OID objects in order to differentiate them from ordinary strings. And make sure to use single quotes ('') around $in, otherwise Perl will try to interpolate $in as a variable (which presumably has nothing in it).
So I assume you want to do something like this:
$db->$collection->find( {
"_id" => {
'$in' => [ MongoDB::OID->new( value => "4f520122ecf6171327000137" ),
MongoDB::OID->new( value => "4f4f49c09d1bd90728000034" )
]
}
} );
Edit: Additionally, using autoloaded method names to retrieve collections has been deprecated for a while. You're better off using $db->get_collection( "collection name" )->find( ... )

MongoDB : use $ positional operator for querying

I have a collection with entries that look like :
{
"userid": 1,
"contents": [
{ "tag": "whatever", "value": 100 },
{"tag": "whatever2", "value": 110 }
]
}
I'd like to be able to query on that collection and returning only one part of the array : the one matching the query. I'm trying to use the $ positional operator to do so but it hasn't worked so far.
Here is more precisely what I'd like to do :
collection.find({'contents.tag':"whatever"},{'contents.$.value':1})
As a result I expect sth with only the value corresponding to the entry in the array that matched query, which is 100 in this case.
Do you know what's wrong ? I was thinking that maybe the $ operator can only be used for update and not for querying. Anyone in the know ?
Thanks !
Yes, you are correct - the positional operator is used for updating an object.
The solution for now would be to return the array an pull the field out in your application.
There is an open enhancement request for this feature (in queries):
https://jira.mongodb.org/browse/SERVER-828
For more information on the positional operator, see:
http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator
What you are looking for is the $elemMatch operator.
It might be an overkill, but I guess you can use map-reduce for this.
first, pre-filter with a query, emit all array elements in map, filter the ones that do not match either in emit or in reduce. If you don't set out everything will happen in RAM.
If you have to run those kinds of queries often, it might be worthwhile to duplicate the data.
Nevertheless, I hope the SERVER-828 will be implemented soon!
Create a query with $in instead and add your equal value to the array, this can solve your issue
$users_array = array(xxxxxxxx,yyyyyy);
$user = Db::find('fb_users', array(
'facebook_id' => array(
'$in' => array($users_array)
)
));

Does MongoDB's Map/Reduce sort work?

If the following is used
Analytic.collection.map_reduce(map, reduce,
:query => {:page => subclass_name},
:sort => [[:pageviews, Mongo::DESCENDING]]).find.to_a
it won't sort by pageviews. Alternatively, if it is array of hash:
Analytic.collection.map_reduce(map, reduce,
:query => {:page => subclass_name},
:sort => [{:pageviews => Mongo::DESCENDING}]).find.to_a
it won't work either. I think the reason it has to be an array is to specify the first field to sort by, etc. I also tried just a flat array instead of an array of array like in the first code listing up above and it didn't work either.
Is it not working? This is the spec: http://api.mongodb.org/ruby/current/Mongo/Collection.html#map_reduce-instance_method
What are you trying to do? Sort is really only useful in conjunction with limit: it's applied before the map so you can just MapReduce the latest 20 items or something. If you're trying to sort the results, you can just do a normal sort on the output collection.
Ok, it is a little bit tricky:
After the map_reduce(), a Mongo::Collection object is returned, but the structure is like:
[{"_id":123.0,"value":{"pageviews":3621.0,"timeOnPage":206024.0}},
{"_id":1320.0,"value":{"pageviews":6584.0,"timeOnPage":373195.0}},
...
]
so to do the sort, it has to be:
Analytic.collection.map_reduce(map, reduce,
:query => {:page => subclass_name}).find({},
:sort => [['value.pageviews', Mongo::DESCENDING]])
note the value.pageviews part.