How to search in fulltext index using php in mongodb - php

I'm using mongodb 2.4 and added fulltext index to the "title" field in one the collection. How should I search something in that field using php?
This is the code I use right now:
$params = array(
'_id' => array(
'$gt' => (int)$gt
)
);
$r = $this->collection->find( $params )->limit($limit);

This seem to be the answer to my question:
<?php
$result = $db->command(
array(
'text' => 'bar', //this is the name of the collection where we are searching
'search' => 'hotel', //the string to search
'limit' => 5, //the number of results, by default is 1000
'project' => Array( //the fields to retrieve from db
'title' => 1
)
)
);
http://www.php.net/manual/en/mongodb.command.php#111891

Related

Add parameter to function php in option query mongo?

My mongoDB have field: $creatime, I want add field to function php.
How do it?
$ops = array(
array('$match' => $query),
array('$addFields' => array('date_code' => functionPHP('$create_time'))),
);
$results = $col->aggregate($ops,array('cursor' => array('batchSize' => 200)));

Merge multidimensional array in PHP

I have a multi array and I need to combine it with other. This array is made for send in XML format to SOAP, so it need to have the correct structure.
This array is like an invoice, it have "items" which i have to repeat. So, i think in make two arrays (one have always the same structure) and add the items array.
The problem is that if I use merge, I could not put the second array in the correct key. Here an example.
This is the correct array structure:
$params = array(
'authRequest' =>
array( 'token' => 'token',
'sign' => 'sign',
'cuitRepresentada' => 'CUIT' ),
'comprobanteRequest' =>
array( 'codigoTipoComprobante' => $codtipcbte,
'numeroPuntoVenta' => $ptovta,
'numeroComprobante' => $cbte,
**'arrayItems' =>
array( 'item' =>
array(
array(
'tipo'=> $compreqitem['tipo'],
'codigoTurismo'=> $compreqitem['codTur'],
'descripcion'=> $compreqitem['descrip'],
'codigoAlicuotaIVA'=> $compreqitem['codAlic'],
'importeIVA'=> $compreqitem['impIva'],
'importeItem'=> $compreqitem['impItem'],
),
array(
'tipo'=> $compreqitem['tipo'],
'codigoTurismo'=> $compreqitem['codTur'],
'descripcion'=> $compreqitem['descrip'],
'codigoAlicuotaIVA'=> $compreqitem['codAlic'],
'importeIVA'=> $compreqitem['impIva'],
'importeItem'=> $compreqitem['impItem'],
),
),
),**
'arraySubtotalesIVA' =>
array( 'subtotalIVA' =>
array(
'codigo'=> $compreqiva['codIva'],
'importe'=> $compreqiva['importe'],
),
),
),
);
So, i build the array with "arrayItems" empty
'arrayItems' => array(),
Then i build the arrayItem array:
$arrayitems =
array('arrayItems' =>
array( 'item' =>
array(
array(
'tipo'=> $compreqitem['tipo'],
'codigoTurismo'=> $compreqitem['codTur'],
'descripcion'=> $compreqitem['descrip'],
'codigoAlicuotaIVA'=> $compreqitem['codAlic'],
'importeIVA'=> $compreqitem['impIva'],
'importeItem'=> $compreqitem['impItem'],
),
array(
'tipo'=> $compreqitem['tipo'],
'codigoTurismo'=> $compreqitem['codTur'],
'descripcion'=> $compreqitem['descrip'],
'codigoAlicuotaIVA'=> $compreqitem['codAlic'],
'importeIVA'=> $compreqitem['impIva'],
'importeItem'=> $compreqitem['impItem'],
),
),
),
);
Then i use merge to join both array:
$resultado = array_merge($params['comprobanteRequest'], $arrayitems);
Works, but the first key is deleted...
'authRequest' =>
array( 'token' => 'token',
'sign' => 'sign',
'cuitRepresentada' => 'CUIT' ),
I dont know why is deleted, maybe the merge function is not the corect way...
Thanks in advance!
If in your first array, arrayItems is always empty, then you don't need a merge, just set the value :
$params['comprobanteRequest']['arrayItems'] = $arrayItems['arrayItems'];
Of course this can be simplified, since $arrayItems contains only one key, but you get the spirit.

MongoDB find query with $and $or and $nearSphere in php

After quite some trying out and web research I go crazy with this query. I want to build a query for 'Clubs' around a geo point (distance max 500 meters) in php on MongoDB.
But when I run query it ignores the distance limit and shows all clubs in database BUT sorted by distance.
Here is my dataset (2dsphere index geoLoc):
{"_id":ObjectId("547c649e30afe32c23000048"),"name":"Club Ritzz","category":"Club","category_list":[{"id":"191478144212980","name":"Night Club"}],"location":{"city":"Mannheim"},"geoLoc":{"type":"Point","coordinates":[8.473665839156,49.484065272756]}}
{"_id":ObjectId("547c649f30afe32c2300004a"),"name":"Das Zimmer Mannheim","category":"Club","category_list":[{"id":"191478144212980","name":"Night Club"}],"geoLoc":{"type":"Point","coordinates":[8.4709362941178,49.487260552592]}}
{"_id":ObjectId("547c64ab30afe32c23000063"),"name":"Nationaltheater Mannheim","category":"Arts/entertainment/nightlife","category_list":[{"id":"173883042668223","name":"Theatre"}],"geoLoc":{"type":"Point","coordinates":[8.4776534992592,49.48782606969]}}
{"_id":ObjectId("547c64a130afe32c2300004f"),"name":"SOHO Bar Club Lounge","category":"Club","category_list":[{"id":"191478144212980","name":"Night Club"},{"id":"164049010316507","name":"Gastropub"}],"geoLoc":{"type":"Point","coordinates":[8.4630844501277,49.49385193591]}}
{"_id":ObjectId("547c64a730afe32c2300005a"),"name":"Loft Club","category":"Club","category_list":[{"id":"191478144212980","name":"Night Club"},{"id":"176139629103647","name":"Dance Club"}],"geoLoc":{"type":"Point","coordinates":[8.4296300196465,49.484211928258]}}
And here my php code (updated Dec-2):
$qry = $pub->find(
array( '$and' =>
array(
array( 'geoLoc' =>
array('$nearSphere' =>
array('$geometry' =>
array('type'=>'Point',
'coordinates'=>
array(
floatval($sLon), floatval($sLat)
)
),
'maxDistance' => 500
)
)
),
array( '$or' =>
array(
array( 'name' => new MongoRegex("/.*club/i")),
array( 'name' => new MongoRegex("/.*zimm/i"))
)
),
array('$or' =>
array(
array('category_list.name' => 'Night Club'),
array('category_list.name' => 'Dance Club'),
array('category' => 'Club')
)
)
)
),
array('id' => 1, 'name' => 1, '_id' => 0)
);
Anyone know why the results are not limited to the specified maxDistance?
I found a similar issue on StackOverflow which outlines that one has to use radians for the maxDistance parameter.
See https://dba.stackexchange.com/questions/23869/nearsphere-returns-too-many-data-what-am-i-missing-am-i-wrong-is-it-a-bug-d
Also it is probably helpful if you'd test the query in mongo shell without using the PHP APIs first (just to see if the query is generally working and append '.explain()' to it to see what generally happens inside DB).

How to use datas one by one from fetching with find('all',) in CakePHP?

I want to match course_id with $courseInfo which is fetching from $cLID.
When I change this $clID['Relationscl']['course_id'] line as 1 everything is ok, but the other way an error appears like Undefined index: Relationscl.
Here is my code:
$this->loadModel('Course');
$this->loadModel('Lecturer');
$clID = $this->Relationscl->find('all', array(
'conditions' => array(
'student_id' => $id
),
'fields' => array(
'course_id','lecturer_id'
)
));
$this->set('clIDs', $clID);
$courseInfo = $this->Course->find('first',array(
'conditions' => array(
'course_id' => $clID['Relationscl']['course_id']
),
'fields' => array(
'course_name','course_code','course_credit'
)
));
$this->set('cInfos', $courseInfo);
Correct me if I'm wrong, but the find('all') function returns and indexed array, so it should be something like
$clID = array( 0 => array('Relationscl'=>array('course_id'=>1 /*and more fields*/)),
1 => array('Relationscl'=>array('course_id'=>2 /*and more fields*/))
/*etc*/);
So clearly $clID['Relationscl'] is undefined. Try with $clID[0]['Relationscl']. Though even that seems weird, why would you do a find('all') if you only plan on using the one record, isn't find('first') better? Or set that $courseInfo definition inside a loop?

cakephp: filtering fields from child table in find('all')

My question extends one posted previously CakePHP: Limit Fields associated with a model. I used this solution effectively for limiting the returned fields for the parent table with this call
$data = $this->SOP10100->find('all',
array('fields' => $this->SOP10100->defaultFields));
However, this method returns the filtered parent and unfiltered child fields. I have 131 child fields of which I only need 7. I have the same defaultFields array construct in the child table. How do I modify this call ( or create a new one) that will return the filtered fields for both parent and child models in the same array?
Here is the structure for the array for the parent table:
public $defaultFields = array(
'SOP10100.SOPNUMBE',
'SOP10100.INVODATE',
'SOP10100.DOCDATE',
'SOP10100.DOCAMNT',
'SOP10100.SUBTOTAL');
Your help is appreciated.
SCORE! Wow, solved two big problems in one day. I finally figured it out with loads of help from many resources:
$this->InvoiceHeader->Behaviors->attach('Containable');
$data = $this->InvoiceHeader->find('all', array(
'fields' => $this->InvoiceHeader->defaultFields,
'contain' => array(
'InvoiceDetail' => array(
'fields' => $this->InvoiceDetail->defaultFields))
)
);
returns my array data just like I want it:
array(
(int) 0 => array(
'InvoiceHeader' => array(
'SOPNUMBE' => 'SVC0202088 ',
'INVODATE' => '2012-04-17 00:00:00',
'DOCDATE' => '2012-04-17 00:00:00',
'DOCAMNT' => '.00000',
'SUBTOTAL' => '.00000'
),
'InvoiceDetail' => array(
(int) 0 => array(
'ITEMNMBR' => 'SERVICE ',
'QUANTITY' => '1.00000',
'UOFM' => 'EA ',
'UNITPRCE' => '.00000',
'TAXAMNT' => '.00000',
'CONTSTARTDTE' => '2012-04-17 00:00:00',
'CONTENDDTE' => '2012-04-30 00:00:00',
'SOPNUMBE' => 'SVC0202088 '
),

Categories