0

I'm trying to insert a record in mongo that expires in sometime.

getMessagesCollection().ensureIndex(new BasicDBObject("createdAt", 1), new BasicDBObject("expireAfterSeconds", 10));

I insert my data like this

Map map = new HashMap();
map.put("_id", mongoMessage.getObjectId());
// Other code
map.put("createdAt", new Date());
getMessagesCollection().insert(convertToDBObject(map))

the field createdAt is a date and before inserting the object looks like this

{ "_id" : "551bf9b72bea951ecf53fc5f" , "createdAt" : { "$date" : "2015-04-01T09:59:19.723Z"} , ...}

But the record is not getting deleted. Can someone tell me what I'm doing wrong?

KS88
  • 147
  • 1
  • 1
  • 8

1 Answers1

0

Looks like you're incorrectly creating the date.

Check out how it's done here

Date now = new Date();
BasicDBObject time = new BasicDBObject("ts", now);
jtmarmon
  • 5,727
  • 7
  • 28
  • 45