Skip to content
3en edited this page Sep 13, 2010 · 2 revisions

How do you make a database query using < and > operators?

Example:
$ouput_query=$this->MODEL->find('all', array('conditions'=>array('FIELD'=>array('$gt'=>10, '$lt'=>20))));
Replace MODEL and FIELD on the above query with the appropriate calls.
‘$gt’ = Greater Than
‘$lt’ = Less Than

More information on MongoDB and conditional operators can be found on (I also assume, but haven’t checked that the other conditional operators listed on MongoDB docs will work in the same manner as $gt and $lt)
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ConditionalOperators

Inserting a nested array
If you want to insert an element into a nested array within a collection you should (in the controller) first get the nested array and then push your new elements on top of the array stack. In this example we use tags.

Example:

First we want to get the existing entry we want to insert a nested element into
$content=$this->Content->find('all', array('conditions'=>array('_id'=>$id)));

We then check if the nested array has been created yet. If not we initialize an empty array variable
if(empty($content['Content']['tags'])){ $temp_tags=array( ); }else{ $temp_tags=$user['Content']['tags']; }
Push the new elements onto the existing array entry $temp_tags
array_push($temp_tags, array( 'tag'=>'blogging', 'date_tag_added'=>date('Y-m-d H:i:s') ));
// Save this new multi dimensional array to the entry
$this->data['Content']['_id']=$user['Content']['_id'] $this->data['Content']['tags']=$temp_tags; $this->Content->save($this->data);

Clone this wiki locally