Searching using a list of IDs in DBIx::Class - perl

I have a list with IDs that are selected by the user.
What is the best way to search all rows using this list of IDs in DBIx::Class??

Use
$rs->search({
whatever_the_column_is => {
'=' => [ #a_bunch_of_ids ]
}
})
or
$rs->search({
whatever_the_column_is => {
-in => [ #a_bunch_of_ids ]
}
})
if your DB likes IN queries better. Both are documented in SQL::Abstract docs.

Related

geoNear with Pagination and sorting on created_at

I´m having an App with the capability of listing articles within a user specified location + radius surrounding. See ruby code with mongo query below.
For Web App, it uses pagination based on "last item" (min_distance) and "excluded_ids" in combination with "limit", what I believe to be the most performant and also better than "limit" and "skip". This solution approach supports "previous page" and "next page". For "jump to page" it makes use of the combination of "limit" and "skip".
geoNear by nature sorts by "distance".
New requirement is to have, additionally, sort by "created_at" (user toggle).
My question:
For sort by "created_at" to work, will it be sufficient to add sort into the query? I´m afraid this won´t work for the current implemented pagination approach and I will need to somehow rewrite to have "last item" based on "created_at", rather than on "min_distance".
Can someone tell for sure how Mongo handles internally the sort and if simply adding sort for created_at will work with this type of pagination?
Or else, how this would look for replacing mind_distance for created_at.
Also, for supporting both sort types, what index/es is/are needed?
One for both, or one for each?
def self.articles_with_distance(params, excluded_ids=[])
if params[:min_distance].present?
Article.collection.aggregate([
{ :"$geoNear" => {
:near => {
:type => "Point",
:coordinates => [params[:lon].to_f, params[:lat].to_f],
},
:query => {_id: { :"$nin" => excluded_ids }}, # to exclude items already shown if min distance between two pages are same
:distanceField => "distance_from",
:maxDistance => params[:radius].to_i,
:minDistance => params[:min_distance].to_i, # only need to reduce the result set by exclude items had been shown previous pages
:spherical => true,
:limit => (params[:per_page].to_i || 10)
}}
]);
else
Article.collection.aggregate([
{ :"$geoNear" => {
:near => {
:type => "Point",
:coordinates => [params[:lon].to_f, params[:lat].to_f],
},
:distanceField => "distance_from",
:maxDistance => params[:radius].to_i,
:spherical => true,
#
}},
{
:"$skip" => (params[:per_page].to_i * (params[:page].to_i-1))
},
{
:"$limit" => (params[:per_page].to_i || 10)
}
]);
end
end

MongoDB aggregation match conditions fails

I have a problem with aggregation function:
this is a screen from MongoDB Compass:
Category structure:
{
'title' => 'Sport'
'articles' => [
{
'title' => 'My title'
'status' => 'published'
}
]
....
}
In the screen you can see a filter in order to get all categories with at least an article with status published.
In the result I always see also category with 'status' => 'draft'. I do not understand why this filter is not working.
How can I solve this?
Thanks

Convert strings to MongoId in array of objects

We have a collection which stores a list of sub objects:
{
_id: MongoID,
title: 'Some title',
items: [
{
sub_type: 'MongoId'
}
]
}
Annoyingly it turns out we've been storing the item.sub_type as both MongoId Objects and as strings.
I've created a query to find all the string versions: (I'm using a regex as some old items were just numbers instead of Ids, they can be ignored).
$query = array(
'items' => array(
'$elemMatch' => array(
'sub_type' => array(
'$type' => 2,
'$regex' => new MongoRegex('/^[a-f\d]{24}$/i')
)
)
)
);
$results = $mongo->collection->find( $query );
I'm now trying to come up with the best way to convert these to MongoIds. I can easily loop through them in PHP and update them, but this seems like a waste.
Is there a better way of doing this?
I just realised I didn't really understand the question. Here goes an update. This one should do it.
Using mongo console...
db.collection.find({}).forEach(function (x) {
x.items.forEach(function (y) {
y.sub_type = ObjectId(y.sub_type);
});
db.collection.save(x);
});

Find query with and operator in PHP

Hi i am working on backend of web application & want to find the documents from mongodb database that contain key active_status with value set to both 1 & 2. With mongodb PHP i am confused of how to find with both parameters in single query.
My query was this:
$mongoDb = MongoDbConnector::getCollection("endusers");
$endUserData = $mongoDb->find(array('active_status' => 1, '$and' => array('active_status' => 2)));
I have to fetch the users whose active_status should be 1 & 2. The above query doesnt seems to work. What is the right one for that?
Thanks on advance for quick response.
You have $and the wrong way around. Both arguments need to be included:
$endUserData = $mongoDb->find(array(
'$and' => array(
array( 'active_status' => 1 )
array( 'active_status' => 2 )
)
));
And since that would only make sense when looking for both elements within an array element, then you should instead use $all, which is shorter syntax:
$endUserData = $mongoDb->find(array(
'active_status' => array( '$all' => array(1,2) )
));
I should add that unless you intend to match a document like this:
{ "active_status" => [1,2] }
The you do not in fact want $and at all, but rather you want $or or better yet $in for multiple possible values on the same field:
$endUserData = $mongoDb->find(array(
'active_status' => array( '$in' => array(1,2) )
));
This matches documents like this:
{ "active_status": 1 },
{ "active_status": 2 }

MongoDB & PHP - Returning a count of a nested array

Imagine I have a MonogDB collection containing documents as follows:
{name: 'Some Name', components: {ARRAY OF ITEMS}}
How can I return the name and the count of items in components?
Do I have to use a map/reduce?
I am using PHP's Mongo extension.
EDIT: Snippet of current code in PHP (working) but I just want count of the components
$fields = array(
'name', 'components'
);
$cursor = $this->collection->find(array(), $fields);
$cursor->sort(array('created_ts' => -1));
if (empty($cursor) == true) {
return array();
} else {
return iterator_to_array($cursor);
}
Thanks,
Jim
You could use map-reduce or you could use a simple group query as follows. Since I am assuming that your name property is a unique key, this should work even though it isn't a reason that you'd normally use the group function:
db.test.group({
key: { name:true },
reduce: function(obj,prev) {
var count = 0;
for(k in obj.components)
count++;
prev.count = count;
},
initial: { count: 0}
});
You mentioned that you have an array of components, but it appears that you are storing components as an object {} and not and array []. That is why I had to add the loop in the reduce function, to count all of the properties of the components object. If it were actually an array then you could simply use the .length property.
In PHP it would look something like this (from the Manual):
$keys = array('name' => 1);
$initial = array('count' => 0);
$reduce =<<<JS
function(obj,prev) {
var count = 0;
for(k in obj.components)
count++;
prev.count = count;
},
JS;
$m = new Mongo();
$db = $m->selectDB('Database');
$coll = $db->selectCollection('Collection');
$data = $coll->group($keys, $initial, $reduce);
Finally, I would strongly suggest that if you are trying to access the count of your components on a regular basis that you store the count as an additional property of the document and update it whenever it changes. If you are attempting to write queries that filter based on this count then you will also be able to add an index on that components property.
You could use db.eval() and write the calculation in JavaScript.
Jim-
These are two separate operations; Unless you want to leverage PHP's count on the results you get which you would then do something like:
$m = new Mongo();
$db = $m->selectDB('yourDB');
$collection = $db->selectCollection('MyCollection');
$cursor = $collection->find(array(), array("name"=>1, "components"=>1));
foreach($cursor as $key){
echo($key['name'].' components: '.count($key['components']);
}
Ran across this today, If your using the new driver with aggregate you can do this in php, ( given this schema )
{name: 'Some Name', components: {ARRAY OF ITEMS}}
In PHP:
$collection = (new Client())->db->my_collection;
$collection->aggregate([
'$match' => ['name' => 'Some Name'],
'$group' => [
'_id' => null,
'total'=> ['$sum' => "\$components"]
]
]);
The trick here with PHP is to escape the $ dollar sign, this is basically what the mongo documentation says when using size or sum
https://docs.mongodb.com/manual/reference/operator/aggregation/size/
https://docs.mongodb.com/manual/reference/operator/aggregation/sum/
The problem I had is mongo puts fields in as "$field" and PHP doesn't like that at all because of the way it does variable interpolation. However, once you escape the $, it works fine.
I think for this particular case you'd need to do something similar but with $project instead of $group Like this
$collection = (new Client())->db->my_collection;
$collection->aggregate([
'$match' => ['name' => 'Some Name'],
'$project' => [
'name' => "\$name",
'total'=> ['$sum' => "\$components"]
]
]);
This is an old question but seeing as there is no answer picked, I'll just leave this here.