phpmongodbcakephp-2.0

How to convert Mongodb query to Cakephp compatible


How to change mongodb command into cakephp script?

I have written this command:

db.citytable.find({
"city": "del%"
 }, {
"cityname": 1
});

I want to convert it in Cakephp. like does not work with Mongodb. I am using Cakephp with MongoDB.

EDIT

public function load_city() {
    $this - > layout = false;
    $city_list = array();
    $this - > autoRender = false;
    if ($this - > request - > is('ajax')) {
        $city_term = strtoupper($this - > request - > query('term'));
        $mydb = new MongoClient();


        $params = array(
            'conditions' = > array(
            'OR' = > array(
            'local_area' = > new MongoRegex("/$city_term/i"),
            'city_name' = > new MongoRegex("/$city_term/i"), ), ), );
        $results = $this - > Managelist - > find('all', $params);
        $this - > set(compact('results'));
        foreach($results as $result) {
            $city_list[] = $result['Managelist']['local_area'].",".$result['Managelist']['city_name'];

            array_unshift($city_list, $result['Managelist']['city_name']);

        }

        echo json_encode(array_unique($city_list));

        exit;

    }
}

I am calling load_city() from my jquery autocomplete.


Solution

  • You can do like this,

        $m = new MongoClient();
        $collection = $m->selectCollection($this->dbName, 'citytable');
        $string="del";
        $regexObj = new MongoRegex("/$string/i");
        $where = array("city" => $regexObj);
        $resultset = $collection->find($where);
    
        $resultData = array();
        while ($resultset->hasNext()) {
            $clientObj = $resultset->getNext();
            $resultData[] = $clientObj;
        }
        var_dump($resultData);