Cakephp aggregation query (MongoDB) - mongodb

Using ichikaway-cakephp I am trying to convert following query (running fine in php) to cakephp
In cakephp it returns empty array
Core PHP
$out = $collection->aggregate(array(
array('$unwind' => '$as'),
array(
'$group' => array(
'_id' => array('as'=>'$as'),
'count' => array('$sum' => 1)
)
)
));
Cakephp
$conditions=array('aggregate'=>array(
array('$unwind' => '$as'),
array(
'$group' => array(
'_id' => array('as'=>'$as'),
'count' => array('$sum' => 1)
)
)
));
$results = $this->Post->find('all',array('conditions'=>$conditions));
I am unable to find aggrgation framework function in test cases
So far only this commit talks about aggregation.

$params = array(
array('$unwind' => '$as'),
array(
'$group' => array(
'_id' => array('as'=>'$as'),
'count' => array('$sum' => 1)
)
)
));
$mongo = $this->Post->getDataSource();
$mongoCollectionObject = $mongo->getMongoCollection($this->Post);
$results = $mongoCollectionObject->aggregate($params);

Related

How to use where/whereDate in mongodb aggregate functions using Jessengers package in Laravel?

How to use where/whereDate in aggregate mongodb query ?
I am using aggregate query in laravel and jessenger-mongodb to group by fields.
The problem here is, I need to add a condition to fetch grouped data of a particular date i.e created_at.
Following code worked perfectly if I do not use pass $date i.e $date = null:
public static function getUsersByTypeLogin($date = null)
{
$q = self::raw()->aggregate(
[
array(
'$match' => array(
'type' => self::LOGIN,
)
),
array(
'$group' => array(
'_id' => array(
'lat' => '$latitude',
'long' => '$longitude',
'country' => '$country',
'state' => '$state'
),
'count' => array('$sum' => 1)
)
)
]
);
if(true == $date){
$q = $q->whereDate('created_at','=',$date);
}
return $q;
}
but If I pass $date it returns an error, it says:
message: "Call to undefined method MongoDB\Driver\Cursor::whereDate()"
$date is a Carbon object e.g:
Carbon #1549843200 {#791
date: 2019-02-11 00:00:00.0 +00:00
}
Please help me in this regards.
Try this.
$q = self::raw(function($collection) use($date) {
return $collection->aggregate([
array(
'$match' => array(
'type' => self::LOGIN,
'created_date' => $date,
)
),
array(
'$group' => array(
'_id' => array(
'lat' => '$latitude',
'long' => '$longitude',
'country' => '$country',
'state' => '$state'
),
'count' => array('$sum' => 1)
)
)
]);
});

Sort with results matching condition on top

I have written mongodb aggregation query in php like below lines of code.
$orrollno= array('$or' => array(array("student.roll_no" => new MongoRegex("/$arg/i"))));
$orlastname= array('$or' => array(array("student.last_name" => new MongoRegex("/$arg/i"))));
$oremail= array('$or' => array(array("student.email" => new MongoRegex("/$arg/i"))));
$orguardian= array('$or' => array(array("student.guardian_name" => new MongoRegex("/$arg/i"))));
$orphone= array('$or' => array(array("student.phone1" => new MongoRegex("/$arg/i"))));
$orfullname= array('$or' => array(array("fullname" => new MongoRegex("/$arg/i"))));
$orfirstmiddle= array('$or' => array(array("firstmiddle" => new MongoRegex("/$arg/i"))));
$orfirstlast= array('$or' => array(array("firstlast" => new MongoRegex("/$arg/i"))));
$query = array( '$or' => array($orrollno,$orlastname,$oremail,$orguardian,$orphone,$orfullname,$orfirstmiddle,$orfirstlast));
$outputTotalResults= $this->db->studentTbl->aggregate(
array(
array(
'$project' => array(
'fullname' => array('$concat' => array('$first_name', ' ', '$middle_name', ' ', '$last_name')),
'firstmiddle' => array('$concat' => array('$first_name', ' ', '$middle_name')),
'firstlast' => array('$concat' => array('$first_name', ' ', '$last_name')),
'student' => '$$ROOT'
)
),
array(
'$match' => $query
),
)
);
I am trying to sort the results which comes from $match => $query.
For e.g $arg contains "William David" then results should first contain the records with names Willian David and then the rest of the results.
Any help shall be greatly appreciated!!!
Based on ur suggestion I have now tried the below
$outputTotalResults= $this->db->studentTbl->aggregate(
array(
array(
'$project' => array(
'fullname' => array('$concat' => array('$first_name', ' ', '$middle_name', ' ', '$last_name')),
'firstmiddle' => array('$concat' => array('$first_name', ' ', '$middle_name')),
'firstlast' => array('$concat' => array('$first_name', ' ', '$last_name')),
'student' => '$$ROOT',
'weight' => array(
'$cond' => array(
array(
'$or' => array(
array('$eq' => array('$fullname' => $arg )),
array('$eq' => array('$firstmiddle' => $arg)),
array('$eq' => array('$firstlast' => $arg)),
)
),
10,
0
)
),
array(
'$sort' => array( 'weight'=> -1 )
),
array(
'$match' => $query
),
)
)
)
);
What you want to achieve here is a "weighted sort", where you essentially want to calculate a field based on conditions and then apply a $sort pipeline stage to that result.
The general case is to apply $cond with a logical condition and either return a value or not, possibly for more than one condition in a cascading way.
Ideally with MongoDB 3.4 and above, use $addFields:
array(
array(
'$addFields' => array(
'weight' => array(
'$cond => array(
array(
'$and' => array(
array( '$eq' => array( '$first_name', 'Willam' ) )
array( '$eq' => array( '$last_name', 'David' ) )
)
),
10,
0
)
)
)
),
array(
'$sort' => array( 'weight'=> -1 )
)
)
Or in prior versions where you cannot simply "append" a new field to the existing document structure you use $project, either specifying each field you want or altering the structure returning under one property via $$ROOT:
array(
array(
'$project' => array(
'first_name' => 1,
'last_name' => 1,
'weight' => array(
'$cond => array(
array(
'$and' => array(
array( '$eq' => array( '$first_name', 'Willam' ) )
array( '$eq' => array( '$last_name', 'David' ) )
)
),
10,
0
)
)
)
),
array(
'$sort' => array( 'weight'=> -1 )
)
)
So in that simple case, whenever the "both" conditions (via $and) are met, the weight property is assigned a value of 10, otherwise it gets a 0. The subsequent sort on the weight property is in "descending" order, so all the 10 values where the condition matched will be on "top", whilst all other results will come after all the matches.
This is how you would structure for your exact implementation. First you $match your query conditions as this reduces the overall documents to process and this is the only time an aggregation pipeline can actually use an index.
Then you $project the field with the comparisons for whether the match phrase was in the preferred fields, and finally $sort on that calculated field.
array(
array( '$match' => $query ),
array(
'$addFields' => array(
'weight' => array(
'$cond => array(
array(
'$or' => array(
array(
'$eq' => array(
array('$concat' => array('$first_name', ' ', '$middle_name', ' ', '$last_name')),
$arg
)
),
array(
'$eq' => array(
array('$concat' => array('$first_name', ' ', '$middle_name')),
$arg
)
),
array(
'$eq' => array(
array('$concat' => array('$first_name', ' ', '$last_name')),
$arg
)
)
)
),
10,
0
)
)
)
),
array(
'$sort' => array( 'weight'=> -1 )
)
)
So always $match first or otherwise use a pipeline stage that is going to use an index and "optimize" your result. Then manipulate and remember that you cannot use calculated fields for comparison in a "single" $project phase. If you really need it then you either duplicate the calculations or do the calculations in one stage and then compare on the values in the next stage.
Honestly though, once you go to these lengths you are basically reproducing what a text search, in which you can:
Spread the index across all the fields you want to search in. This eliminates the massive $or condition into a simple query operation.
Specify a weighting on the particular fields where the match would be more important.
The only case where a "text search" would not be the best solution is if the fields you want "more weight on" change on a regular basis. Since text indexes have "set" values for the weighting and you can have only one per collection, then you cannot easily change the combination of fields to assign more weight to. With the aggregation process shown, its fairly trivial to change around the fields and assignment of weight.

How to get distinct documents by some field in mongodb phalcon?

In mongo shell the query:
db.collections.distinct('user_id');
simply gives the distinct documents.
I am working on phalcon and there is no option like
Collections::distinct()
So how do i query distinct in phalcon. Please help me
Perhaps Phalcon's answer in the forum could help: https://forum.phalconphp.com/discussion/6832/option-for-function-distinct-phalconmvccollection
They suggest using aggregations:
$data = Article::aggregate(
array(
array(
'$project' => array('category' => 1)
),
array(
'$group' => array(
'_id' => array('category' => '$category'),
'id' => array('$max' => '$_id')
)
)
)
);

How to merge query in wordpress with different arguments

$all = array(
'post_type' => array("pwpd_poll","pwpd_infog"),
'posts_per_page' => wp_is_mobile() ? 3 : 7,
'orderby' => 'post_date',
'order' => 'DESC'
);
$news = array(
'post_type' =>array('pwpd_news'),
'meta_query' => $meta_query_par,
'date_query'=>array(
array(
'year' => $today['year'],
'month' => $today['mon'],
'day' => $today['mday'],
)
)
);
the problem is the $news output is always in the end of the loop the "orderby" doesnt seem to be working and also the "posts_per_page" is not exact in the output.

MongoDB keeps giving me null

I have some difficulty in getting Mongodb aggregate to work. It keeps giving me null. Please help. Below are the codes written in php. Thanks.
What I want to do is to sum up the values of 2 fields, Requests and Responses, between 2 particular dates
try {
$mongodb = new MongoClient("mongodb://ad:pass2word1#localhost");
$database = $mongodb->selectDB('backend');
$collection = new MongoCollection($database, 'RequestSummary');
$pipeline = array(
array(
'$group' => array(
'_id' => array(
'request' => array('$sum' => '$Requests'),
'response' => array('$sum' => '$Responses')
)
)
),
array(
'$match' => array(
'RequestDate' => array(
'$gte' => intval($_SESSION['range_from']),
'$lte' => intval($_SESSION['range_to'])
)
)
)
);
$collection->aggregate($pipeline);
var_dump($g);
} catch (MongoConnectionException $exc) {
echo $exc->getTraceAsString();
}
The _id of your $group can't contain aggregation operators like $sum. Those sums need to be defined as fields at the same level as _id. If you don't want to group on a specific field you can use NULL for the _id like this:
array(
'$group' => array(
'_id' => NULL,
'request' => array('$sum' => '$Requests'),
'response' => array('$sum' => '$Responses')
)
),