how to delete records in mongo collection based on epoch?

Viewed 22

below is my mongo db collection:

enter image description here

so here i need to query in mongo db through java,

to remove the records of (current epoch-24 hours), basically i need last 24 hours records

like in this : db data, the first row is last 24 hours so after removal the db should be like this:

enter image description here

can you pls help how to do this in java with mongo?

assuming i have the DAO and POJO available .

1 Answers
import java.util.Arrays;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.conversions.Bson;
import java.util.concurrent.TimeUnit;
import org.bson.Document;

/*
 * Requires the MongoDB Java Driver.
 * https://mongodb.github.io/mongo-java-driver
 */

Bson filter = new Document("$and", Arrays.asList(new Document("timeStamp", 
        new Document("$gt", "now-24 epoch")), 
        new Document("$lt", "now epoch")));

MongoClient mongoClient = new MongoClient(
    new MongoClientURI(
        "[YOUR_MONGO_URI]"
    )
);
MongoDatabase database = mongoClient.getDatabase("[YOUR_DB]");
MongoCollection<Document> collection = database.getCollection("[YOUR_COLLECTION]");
FindIterable<Document> result = collection.find(filter);
Related