hibernate 如何根据纪元删除Mongo集合中的记录?

sqougxex  于 2022-11-14  发布在  Go
关注(0)|答案(1)|浏览(123)

以下是我的Mongo数据库收藏:

所以在这里我需要通过Java在mongo db中进行查询,

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

如下所示:db data,第一行是最后24小时,因此删除后的db应该如下所示:

你能帮我在Java中用mongo做这件事吗?
假设我有DAO和POJO可用。

l0oc07j2

l0oc07j21#

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

相关问题