Since MongoDB 2.2 it is possible to " Expire Data from Collections Using a TTL " which is implemented as a special index type.
The latest version of Doctrine ORM supports this Index Option . Unfortunately I have not been able to find how to correctly set this index using doctrine annotations/config files.
This is how I'm attempting to do it, I hope some one can help me setting it correctly:
<?php
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
/**
* @MongoDB\Document(collection="log")
* @MongoDB\Indexes({
* @MongoDB\Index(keys={"expiration"=1, "expireAfterSeconds"=30})
* //... other indexes go here
* })
*
*/
class Log
{
// ...
/**
* @MongoDB\Date
*/
protected $expiration;
// ...
}
Then when I execute php app/console doctrine:mongodb:schema:update
(in symfony 2.3.x) The index is generated, but the index generated doesn't look correct.
this is what I get when I execute db.system.indexes.find();
in my database:
{ "v" : 1, "key" : { "expiration" : 1, "expireAfterSeconds" : 30 }, "ns" : "mydatabase.log", "sparse" : false, "name" : "expiration_1_expireAfterSeconds_-1" }
which is not the same as if I create the index manually:
db.log.ensureIndex( { "expiration": 1 }, { expireAfterSeconds: 30 } );
as it generates the following index:
{ "v" : 1, "key" : { "expiration" : 1 }, "ns" : "mydatabase.log", "name" : "expiration_1", "expireAfterSeconds" : 30 }
How would I setup TTL on a date field using doctrine annotations/config files?
You're mixing index options with the keys
parameter. The Index
annotation also has an options
parameter. See the example in the Index annotation docs :
<?php
/**
* @Document(
* indexes={
* @Index(keys={"username"="desc"}, options={"unique"=true})
* }
* )
*/
class User
{
//...
}
This is equivalent to passing {unique: true}
as the second argument db.collection.ensureIndex()
. You can replace unique
with expireAfterSeconds
to create a TTL index.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.