You are browsing a version that is no longer maintained. |
Indexes
Working with indexes in the MongoDB ODM is pretty straight forward. You can have multiple indexes, they can consist of multiple fields, they can be unique and you can give them an order. In this chapter we'll show you examples of indexes using annotations.
First here is an example where we put an index on a single property:
Index Options
You can customize the index with some additional options:
- name - The name of the index. This can be useful if you are
- indexing many keys and Mongo complains about the index name being
- too long.
- - dropDups - If a unique index is being created and duplicate
- values exist, drop all but one duplicate value.
- - background - Create indexes in the background while other
- operations are taking place. By default, index creation happens synchronously. If you specify TRUE with this option, index creation will be asynchronous.
- - safe - You can specify a boolean value for checking if the
- index creation succeeded. The driver will throw a MongoCursorException if index creation failed.
- -
- expireAfterSeconds - If you specify this option then the associated
- document will be automatically removed when the provided time (in seconds)
- has passed. This option is bound to a number of limitations, which
- are documented at http://docs.mongodb.org/manual/tutorial/expire-data/.
- -
- order - The order of the index (asc or desc).
- -
- unique - Create a unique index.
- -
- sparse - Create a sparse index. If a unique index is being created the sparse option will allow duplicate null entries, but the field must be unique otherwise.
Unique Index
For your convenience you can quickly specify a unique index with
@UniqueIndex
:
If you want to specify an index that consists of multiple fields you can specify them on the class doc block:
To specify multiple indexes you must use the @Indexes
annotation:
Embedded Indexes
You can specify indexes on embedded documents just like you do on normal documents. When Doctrine creates the indexes for a document it will also create all the indexes from its mapped embedded documents.
Now if we had a BlogPost
document with the Comment
document embedded many times:
If we were to create the indexes with the SchemaManager
:
It will create the indexes from the BlogPost
document but will also create the indexes that are
defined on the Comment
embedded document. The following would be executed on the underlying MongoDB
database:
Also, for your convenience you can create the indexes for your mapped documents from the console:
If you are mixing document types for your embedded documents, ODM will not be able to create indexes for their fields unless you specify a discriminator map for the embed-one or embed-many relationship. |
Geospatial Indexing
You can specify a geospatial index by just specifying the keys and options structures manually:
1 <?php
/**
* @Document
* @Index(keys={"coordinates"="2d"})
*/
class Place
{
/** @Id */
public $id;
/** @EmbedOne(targetDocument="Coordinates") */
public $coordinates;
}
/** @EmbeddedDocument */
class Coordinates
{
/** @Float */
public $latitude;
/** @Float */
public $longitude;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Requiring Indexes
Sometimes you may want to require indexes for all your queries to ensure you don't let stray unindexed queries make it to the database and cause performance problems.
When you run queries it will check that it is indexed and throw an exception if it is not indexed:
When you execute the query it will throw an exception if city
was not indexed in the database. You can control
whether or not an exception will be thrown by using the requireIndexes()
method:
You can also check if the query is indexed and with the isIndexed()
method and use it to display your
own notification when a query is unindexed:
If you don't want to require indexes for all queries you can set leave requireIndexes
as false and control
it on a per query basis: