Skip to content

How to execute MongodbSource methods such as "ensureIndex()"

ichikaway edited this page Aug 23, 2011 · 4 revisions

MongodbSource has a lot of methods such as "ensureIndex()". How do I execute MongodbSource::ensureIndex() ?

Here is a sample.

In posts_controller.php (Model is PostModel).

<?php
class PostsController extends AppController {
    public function createindex() {

        $mongo = $this->Post->getDataSource();
        $mongo->ensureIndex($this->Post, array('title' => 1));

    }
}

You can see index in post collection with mongo shell.

$mongo
> use cakemongo
> db.posts.getIndexes()
[
        {
                "name" : "_id_",
                "ns" : "cakemongo1.posts",
                "key" : {
                        "_id" : 1
                }
        },
        {
                "_id" : ObjectId("4d0b1824a4cf2bda690d0000"),
                "ns" : "cakemongo1.posts",
                "key" : {
                        "title" : 1
                },
                "name" : "title_1"
        }
]