5

I have a problem with the "time to live" settings in MongoDB. I created an Indexed Annotation in my Spring-Boot 2.0.2.RELEASE project in my Entity which represents my Document in the MongoDB. I set the "expireAfterSeconds" for testing to 15 seconds but MongoDB does not delete a inserted Document after 15 seconds. Can someone tell me what I'm doing wrong?

This is the MongoDB Index as JSON:

[
  2,
  {
    "createdDateTime" : 1
  },
  "deleteAt",
  "AccountServiceDB.AccountRegistration",
  NumberLong(15)
]

This is my entity:

@Document(collection = "AccountRegistration")
public class UserRegistration {

  @Id
  private ObjectId _id;
  @Indexed(unique = true)
  private String username;

  @Indexed(unique = true)
  private String email;

  private String user_password;

  @Indexed(name = "deleteAt", expireAfterSeconds = 15)
  private Date createdDateTime;

  public UserRegistration() {}

  public ObjectId get_id() {
    return _id;
  }

  public void set_id(ObjectId _id) {
    this._id = _id;
  }
}
Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
iSmo
  • 190
  • 3
  • 11

3 Answers3

0

Here is explanation: "The background task that removes expired documents runs every 60 seconds. As a result, documents may remain in a collection during the period between the expiration of the document and the running of the background task."

JJussi
  • 1,540
  • 12
  • 12
  • This isn't the answer, you're just stating the documents may live past the TTL. If you replicate the code the developer is using, you'll see that the document lives forever and is never deleted. Therefore your answer isn't actually useful to the developer, and should have been a comment instead. – Chris Jun 14 '18 at 20:28
0

First check that the index is properly created. Using mongo-shell, you can do db.collection.getIndexes() which should return the given collection's indexes along the TTL index e.g.

[
    {
        "v" : 2,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "sample.collectionsName"
    },
    {
        "v" : 2,
        "key" : {
            "dateField" : 1
        },
        "name" : "dateField",
        "ns" : "sample.collectionname",
        "expireAfterSeconds" : NumberLong(5)
    }
]

If this doesn't work, you'll need to enable index creation with

      auto-index-creation: true

After all this is set, you should see your documents are getting deleted indeed, however take the mongod-ttl job into consideration. This job runs every 60 seconds, which means that your documents with the TTL index could initially miss the first cleanup run and would require +60seconds to be deleted.

This is perfectly described in the docs that JJussi pinned as well.

TTL Indexes - MongoDB

eja
  • 4,677
  • 3
  • 21
  • 30
0

I don't know if this is still relevant, but I came across this issue just yesterday.

eja's answer is correct though unlikely to be quite what OP is looking for since he's interfacing to mongo using spring-boot.

I've found two approaches:

  • Configure spring-mongo to auto create indexes (i.e. any field annotated using @Indexed. By default this is off (which is a tad confusing). Enable using:

    @Configuration
    public class MongoConfig extends AbstractMongoClientConfiguration {
        ...
    
        // Enabled auto index creation wherever annotated.
        @Override
        protected boolean autoIndexCreation() {
            return true;
        }
     }
    
    

    You can check this easily using mongo-compass, say.

    This enables indexes across your project. That's a good thing, though for me it affected app startup times a ton, which for dev I didn't like that much.

  • Using @Autowired MongoOperations mongoOperations:

    // Set index based expiry in seconds.
    mongoOperations.indexOps(MyDocument.class)
        .ensureIndex(new Index()
            .on("createdAt", Sort.Direction.ASC).expire(10));
    

    This allows division of logically required indexes (e.g. expiry via index), and nice-to-have indexes for prod.

See also https://www.baeldung.com/spring-data-mongodb-index-annotations-converter

jpd
  • 41
  • 3