I want to use aggregate for group but how do I use laravel-mongodb like this:
// col
$pipeline = $this->getCollection() -> createPipeline();
$pMatchArr = array(
'ij_statistics_time' => array(
'$gte' => $_intStartTime,
'$lte' => $_intEndTime
)
);
if(!empty($_intUid)){
$pMatchArr['ij_uid'] = $_intUid;
}
$pipeline->match($pMatchArr);
// get date
$pipeline = $pipeline->group(
array(
'_id'=>'$ij_uid',
'level_one_count'=>array( '$sum'=>'$ij_level_one_new_num' ),
'level_two_count'=>array( '$sum'=>'$ij_level_two_new_num' ),
'level_three_count'=>array( '$sum'=>'$ij_level_three_new_num' ),
'level_four_count'=>array( '$sum'=>'$ij_level_four_new_num' ),
'level_five_count'=>array( '$sum'=>'$ij_level_five_new_num' ),
'level_six_count'=>array( '$sum'=>'$ij_level_six_new_num' ),
'level_seven_count'=>array( '$sum'=>'$ij_level_seven_new_num' ),
'level_eight_count'=>array( '$sum'=>'$ij_level_eight_new_num' ),
'level_nine_count'=>array( '$sum'=>'$ij_level_nine_new_num' ),
'level_ten_count'=>array( '$sum'=>'$ij_level_ten_new_num' ),
)
);
$pipeArr = $pipeline->aggregate();
return $pipeArr;
It looks like it does not work
$pipeArr = $cursor->groupBy( 'ij_uid' )
->aggregate( 'sum' , $fileds)
->get( $fileds );
return $pipeArr;
I'm using lumen 5.2 and jenssegers/laravel-mongodb
I use the following methods to solve the problem, can be used in the orm native statements
$pipeArr = self::raw( function($collection) use ($match ) {
return $collection->aggregate([
['$match' => $match],
[
'$group' => [
'_id' => '$ij_uid',
'level_one_count' => [ '$sum' => '$ij_level_one_new_num'],
'level_two_count' => [ '$sum' => '$ij_level_two_new_num'],
'level_three_count' => [ '$sum' => '$ij_level_three_new_num'],
'level_four_count' => [ '$sum' => '$ij_level_four_new_num' ],
'level_five_count' => [ '$sum' => '$ij_level_five_new_num' ],
'level_six_count' => [ '$sum' => '$ij_level_six_new_num' ],
'level_seven_count' => [ '$sum' => '$ij_level_seven_new_num' ],
'level_eight_count' => [ '$sum' => '$ij_level_eight_new_num' ],
'level_nine_count' => [ '$sum' => '$ij_level_nine_new_num' ],
'level_ten_count' => [ '$sum' => '$ij_level_ten_new_num' ]
]
],
['$sort' => ['ij_uid' => 1]],
]);
});
return $pipeArr;
Related
I have two tables in mongodb database
activityCountTbl contains data like
{
"_id": ObjectId("5c234f7e3250041280000ca3"),
"activityId": ObjectId("5c0e27ee590a06bf08003c33"),
"weekgroupId": ObjectId("5bfbddbcbb2c5645f8001495"),
"squadronId": ObjectId("5bfc7b7ebb2c56c320002a0a"),
"attendingCount": NumberInt(6),
....
}
{
"_id": ObjectId("5c234f7e3250041280000ca3"),
"activityId": ObjectId("5c0e27ee590a06bf08003c33"),
"weekgroupId": ObjectId("5bfbddbcbb2c5645f8001496"),
"squadronId": ObjectId("5bfc7b7ebb2c56c320002a0a"),
"attendingCount": NumberInt(6),
....
}
squadronTbl contains data like
{
"_id": ObjectId("5c19ccb7590a060691000554"),
"squadronCode": "336",
"squadronName": "336TRS",
}
{
"_id": ObjectId("5c19ccb7590a060691000556"),
"squadronCode": "337",
"squadronName": "337TRS",
}
I am storing count details of a particular activity of a weekgroup in activityCountTbl. I am performing lookup on squadronTbl with activityCountTbl
for fetching squadrons details of a particular weekgroup. The below code is not working.
When I comment/delete the $query code, it fetches all the squadrons of all weekgroups.
$query = ["ActivityArray.weekgroupId" => new MongoDB\BSON\ObjectID("5bfbddbcbb2c5645f8001495"), "ActivityArray.funRun" => "Yes"];
$pipeline = array(
[
'$match' => $query
],
[
'$lookup' => [
'from' => 'activityCountTbl',
'localField' => '_id',
'foreignField' => 'squadronId',
'as' => 'ActivityArray'
]
],
['$project' => [
'_id' => 1.0,
'squadronName' => 1.0,
'ActivityArray' => 1.0
]],
);
return $this->db->squadronTbl->aggregate($pipeline)->toArray();
Please help !!!
$query = ["ActivityArray.weekgroupId" => new MongoDB\BSON\ObjectID("5bfbddbcbb2c5645f8001495"), "ActivityArray.funRun" => "Yes"]
$pipeline = array(
[
'$match' => []
],
[
'$lookup' => [
'from' => 'activityCountTbl',
'localField' => '_id',
'foreignField' => 'squadronId',
'as' => 'ActivityArray'
]
],
[
'$match' => $query
],
['$project' => [
'_id' => 1.0,
'squadronName' => 1.0,
'ActivityArray' => 1.0
]],
);
something like that
I have a collection studentTbl which contains records like
Record 1
"_id": ObjectId("5b45d89bbbc51e2c2c006973")
"first_name": "Pooja",
...
contact_details[
{
..
},
{
..
}
]
transport_details[
{
allotment_id:"68998546878",
..
status:"Inactive"
},
{
allotment_id:"25799856890",
..
status:"Active"
}
]
}
Record 2
"_id": ObjectId("5b45d89bbbc51e2c2533")
"first_name": "Poornima",
...
contact_details[
{
..
},
{
..
}
]
transport_details[
{
allotment_id:"68998546878",
..
status:"Inactive"
}
]
}
Record 3
"_id": ObjectId("5b45d89bbbc51e2c2c00646")
"first_name": "Poonam",
...
contact_details[
{
..
},
{
..
}
]
transport_details[
{
allotment_id:"68998546878",
..
status:"Inactive"
},
{
allotment_id:"25799856890",
..
status:"Active"
}
]
}
I am trying to tweak the below lines of code in order to fetch those students whose first_name or middle_name or last_name contains "poo" and inside the last element of embedded document transport_details, status should be "Active". How to write such a query which will find students on the name basis and traverse through the last element of embedded document transport_details and checks whether the status is active? For e.g in the above collection, pooja and poonam should be returned.
The code is like
// default if nothing is filled
$query = array("schoolId"=> new MongoDB\BSON\ObjectID($this->schoolId));
// if name = filled, class = null, year = null
if(!empty($this->name) && empty($this->academicYearName) && empty($this->programId))
{
$param = preg_replace('!\s+!', ' ', $this->name);
$arg = trim($param);
$query = array(
'$or' => array(
array("first_name" => new MongoDB\BSON\Regex($arg, 'i')),
array("middle_name" => new MongoDB\BSON\Regex($arg, 'i')),
array("last_name" => new MongoDB\BSON\Regex($arg, 'i')),
array("registration_temp_perm_no" => $arg)
),
"schoolId"=> new MongoDB\BSON\ObjectID($this->schoolId)
);
}
...
...
...
$pipeline = array(
array(
'$match' => $query
),
array(
'$lookup' => array(
'from' => 'programTbl',
'localField' => 'classId',
'foreignField' => '_id',
'as' => 'ClassDetails'
)
),
);
try
{
$cursor = $this->collection->aggregate($pipeline);
}
catch (Exception $e) {
}
return $cursor->toArray();
Note that the actual code contains more conditional $query variable...
You can use below query in 3.6.
array(
'$or' => array(
array("first_name" => new MongoDB\BSON\Regex("/poo/i")),
array("middle_name" => new MongoDB\BSON\Regex("/poo/i")),
array("last_name" => new MongoDB\BSON\Regex("/poo/i"))
),
"$expr" => array(
"$eq" => array(
array("$arrayElemAt" => array("$transport_details.status", -1)),
"Active"
)
)
);
You can add below stages in your pipeline
array(
"$match" => array(
"$expr" => array(
"$and" => [
array(
"$or" => [
array( "$eq" => [ array( "$strcasecmp" => [ "$first_name", "Poo" ] ), 1 ] ),
array( "$eq" => [ array( "$strcasecmp" => [ "$last_name", "Poo" ] ), 1 ] ),
array( "$eq" => [ array( "$strcasecmp" => [ "$middle_name", "Poo" ] ), 1 ] ),
],
),
array(
"$eq" => [
array( "$arrayElemAt" => [ array( "$slice"=> [ "$transport_details.status", -1 ] ), 0 ] ),
"Active"
]
)
]
)
)
)
I want to set different focus areas (cropVariants) for different content elements.
I found a solution for this here: https://docs.typo3.org/typo3cms/extensions/core/8.7/Changelog/8.6/Feature-75880-ImplementMultipleCroppingVariantsInImageManipulationTool.html
This works for the standard ctypes like textmedia but not for my own content element. Does anybody have a idea what the problem could be?
As I found out via Slack channel you can achieve that by overriding TCA as follows:
<?php
$originalTtContent = $GLOBALS['TCA']['tt_content'];
$overridesForTtContent = [
'types' => [
'ENTER_YOUR_CTYPE' => [
'columnsOverrides' => [
'ENTER_YOUR_IMAGE_FIELD' => [
'config' => [
'overrideChildTca' => [
'columns' => [
'crop' => [
'config' => [
'cropVariants' => [
'CROPVARIANT_TO_DISABLE' => [
'disabled' => true,
],
'YOUR_NEW_CROPVARIANT' => [
'title' => 'YOUR_NEW_CROPVARIANT',
'allowedAspectRatios' => [
'1:1' => [
'title' => 'Square',
'value' => 1 / 1
],
],
'selectedRatio' => '1:1',
'cropArea' => [
'x' => 0.0,
'y' => 0.0,
'width' => 1.0,
'height' => 1.0,
],
],
],
],
],
]
]
]
]
]
]
]
];
$GLOBALS['TCA']['tt_content'] = array_merge_recursive($originalTtContent, $overridesForTtContent);
Thanks to #kevin-appelt!
I'm using aggregate with the following code:
$result = \DB::connection('mongodb')->collection('urls')->raw(function($collection) use ($alias)
{
$from = new \MongoDate( strtotime('2015-01-01 00:00:00') );
$to = new \MongoDate( strtotime('2015-01-31 00:00:00') );
return $collection->aggregate([
['$match' =>
[
'alias' => $alias,
'clicks.created_at' => [
'$lte' => $to,
'$gte' => $from
]
]
],
['$unwind' => '$clicks'],
['$project' => [
'clicks' => 1
]
],
['$group' => [
'_id' => [ 'day' => [ '$dateToString' => [ 'format' => '%Y-%m-%d', 'date' => '$clicks.created_at' ] ] ],
'count' => [ '$sum' => 1 ]
]
],
]);
});
return $result;
Though I do have records within this timespan, no records are returned. any idea why?
When i want to access to backend yii2 always redirect me to frontend.
Example:
I have installed the yii2-user (dektrium) module in frontend and backend and yii2-admin (mdm) module only in backend . And when i want to acess to
http://localhost/american_eshop/yii-application/frontend/web/**user/admin/index**"
yii2 in the first place accessing to this route and after redirect me to "http://localhost/american_eshop/yii-application/frontend/web/".
I.E. i can get access to resourse but it always redirect me and i cannot undestand where i am in my site.
Sorry for my poor english...
backend cofigs:
main.php
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [],
'components' => [
'user' => [
'identityClass' => 'dektrium\user\models\User',
'enableAutoLogin' => true,
],
'authManager' => [
'class' => 'yii\rbac\DbManager'
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
];
main-local:
<?php
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'QuXcvHHNSiT3mEGgBln9c85IYF7uVkoU',
],
],
'modules' => [
'user' => [
'class' => 'backend\modules\user\Module',
'viewPath' => '#dektrium/user/views',
'enableUnconfirmedLogin' => true,
'confirmWithin' => 21600,
'cost' => 14,
'admins' => ['Admin']
],
]
];
if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = 'yii\gii\Module';
}
return $config;
frontend:
main:
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'frontend\controllers',
'components' => [
'user' => [
'identityClass' => 'dektrium\user\models\User',
'enableAutoLogin' => true,
],
'authManager' => [
'class' => 'yii\rbac\DbManager'
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
],
'params' => $params,
];
main-local:
<?php
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'xlEFSbBB0pjvAJHtEOfY6r5BhDOTIAtB',
],
],
'modules' => [
'user' => [
'class' => 'frontend\modules\user\Module',
'viewPath' => '#dektrium/user/views',
'enableUnconfirmedLogin' => true,
'confirmWithin' => 21600,
'cost' => 14,
'admins' => ['Admin']
],
],
];
if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = 'yii\gii\Module';
}
return $config;