简体   繁体   中英

how to delete records in mongo collection based on epoch?

below is my mongo db collection:

在此处输入图像描述

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:

在此处输入图像描述

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

assuming i have the DAO and POJO available.

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);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM