search and return objects in arrays mongoDB - mongodb

I'm using laravel-mongoDB
how can I search in attraction and blog titles and only retrieve the object, not whole record.
I've tried
[
'$match' => [
"attraction.title" =>
['$regex' => '.*' . $request- >search . '.*']
],
],
result was whole record.
for example if search is first, output should be :
[{title: "first attraction", complete: false},{title: "first blog", complete: false}]
and this is records
[attraction: [{title: "first attraction", complete: false}, {title:"second attraction",
complete: true}]
blog: [{title: "first blog", complete: false}, {title: "second blog", complete: false}]
title: "test"
type: "internal"]

I didn't get the result with only just query but hope it helps
$data = Model::raw(function ($collection) use ($request) {
return $collection->aggregate([
['$facet' =>
[
'attr' => [
[
'$match' => [
"attraction.title" => ['$regex' => '.*' . $request->search . '.*'],
],
],
[
'$unwind' => '$attraction'
],
[
'$project' => [
'title' => '$attraction.title',
]
],
[
'$match' => [
"title" => ['$regex' => '.*' . $request->search . '.*'],
],
]
],
'blog' => [
[
'$match' => [
"blog.title" => ['$regex' => '.*' . $request->search . '.*'],
],
],
[
'$unwind' => '$blog'
],
[
'$project' => [
'title' => '$blog.title',
]
],
[
'$match' => [
"title" => ['$regex' => '.*' . $request->search . '.*'],
],
]
]
]
],
]
);
});
$data will be like:
{
attr:[
{title:...},
{title:...}
],
blog:[
{title:...},
{title:...}
]
}
and use iterator_to_array and foreach to get final result

Related

$match query not working for lookup array in mongodb

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

How to set focusArea for only one specific ctype?

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!

lumen and mongodb and aggregate

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;

MongoDB doesnt return results or ignoring lte / gte

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?

dereference xml::simple output which is complex data structure that is a hash of an array of hashs

I am trying to parse xml using xml::simple .Here is the output of xml::simple
$VAR1 = {
'soapenv:Body'=>[
{
'ns1:queryResponse'=>[
{
'ns1:result'=>[
{
'ns1:done'=>['true'],
'ns1:queryLocator' => [
{
'xsi:nil' => '1',
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
}
],
'ns1:size' => [
'60'
],
'ns1:records' => [
{
'ns2:RefundTransactionTime' => [
'2013-09-12T13:17:18.000-08:00'
],
'xmlns:ns2' => 'http://object.abccom/',
'ns2:MethodType' => [
'CreditCard'
],
'ns2:Gateway' => [
'Chase Paymentech'
],
'ns2:Type' => [
'Electronic'
],
'ns2:RefundDate' => [
'2013-09-12T00:00:00'
],
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'ns2:Status' => [
'Processed'
],
'ns2:Id' => [
'2c92c0f8410f4d9a014113d2170a2e17'
],
'xsi:type' => 'ns2:Refund',
'ns2:AccountId' => [
'2c92c0f9410f55ee0141132b6c936e15'
],
'ns2:Amount' => [
'99'
],
'ns2:CreatedDate' => [
'2013-09-12T13:17:17.000-08:00'
]
},
{
'xmlns:ns2' => 'http://object.abccom/',
'ns2:MethodType' => [
'CreditCard'
],
'ns2:Type' => [
'External'
],
'ns2:RefundDate' => [
'2013-10-12T00:00:00'
],
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'ns2:Status' => [
'Processed'
],
'ns2:Id' => [
'2c92c0f8410f4d9a0141145bfbb61a9b'
],
'xsi:type' => 'ns2:Refund',
'ns2:AccountId' => [
'2c92c0f8410f4d8701411411a9ad79b7'
],
'ns2:Amount' => [
'477.74'
],
'ns2:CreatedDate' => [
'2013-09-12T15:47:54.000-08:00'
],
'ns2:Comment' => [
'16 Payment Exception - Chargeback'
]
}
]
}
],
'xmlns:ns1' => 'http://api.abccom/'
}
]
}
],
'xmlns:soapenv' => 'http://schemas.xmlsoap.org/soap/envelope/'
};
I am using below code :
#!/usr/bin/env perl
use strict;
use Data::Dumper;
use XML::Simple qw(:strict);
my $data = XMLin('Refund.xml', forcearray=>1, keyattr=>[] );
print "Reference type in data is : ", ref($data), "\n";
print Dumper($data);
#Try to access the values
my $records=$data->{"soapenv:Body"}->[0]->{"ns1:queryResponse"}->[0]->{"ns1:result"}->
[0]->{"ns1:records"};
foreach my $record ( #{ $records } ) {
print $record->{"ns2:RefundTransactionTime"};
print "\n";
}
print Dumper($data) generates hash reference containing array of hashes.
I want to format the hash reference generated above to the array of array references format as shown below:
[
[
"AccountId",
"Id",
"Amount",
"CreatedDate",
"Comment",
"Status",
"Type",
"RefundDate",
"MethodType"
],
[
"2c92c0f8410f4d8701411411a9ad79b7",
"2c92c0f8410f4d9a0141145bfbb61a9b",
"477.74",
"2013-09-12T15:47:54.000-08:00",
"16 Payment Exception - Chargeback",
"Processes",
"External",
"2013-10-12T00:00:00",
"CreditCard"
],
[
"2c92c0f9410f55ee0141132b6c936e15",
"2c92c0f8410f4d9a014113d2170a2e17",
"99",
"2013-09-12T13:17:17.000-08:00",
"",
"Processed",
"Electronic",
"2013-09-12T00:00:00",
"Chase Paymentech"
]
],
Any help is greatly appreciated. Thank you
The return value ARRAY(0x1f9fca98) indicates that $record->{"ns2:RefundTransactionTime"} contains an array reference. You probably want the first element: $record->{"ns2:RefundTransactionTime"}->[0].