We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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



98.9k

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



8.7k

Thanks, Any reason for not supporting it?



8.7k
Accepted
answer

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

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

Regards