Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to add indexes for MongoDB collection?

Hello

I'm new to both Phalcon and MongoDB.

I'd like to add indexes to a collection, let's say for the purpose of preventing duplicated email for instance.

In MongoDB I understand we can use ensureIndex, but where to use this in Phalcon?

Thanks in advance. Regards



82.8k

Phalcon does not support ensureIndex but you can use the method available in the MongoDB collection: http://www.php.net/manual/en/mongocollection.ensureindex.php



5.1k

Thanks, Any reason for not supporting it?



5.1k
Accepted
answer

Supposedly, we added mongo in the bootstraper file, then we can use:

$this->mongo->selectCollection('tags')->createIndex(array('tag' => 1));

Regards