[英]Query the most recent entries in a collection in Java
下面的此mongodb查詢將最新條目返回到集合中
db.RSS.find().limit(6).sort({$natural:-1}).pretty()
有誰知道如何在Java中實現此查詢?
使用Mongo-Java驅動程序,此代碼是一個示例:
MongoClient client = new MongoClient("localhost",27017);
MongoDatabase db = client.getDatabase("test");
MongoCollection<Document> collection = db.getCollection("RSS");
FindIterable<Document> it = collection.find().limit(6).sort(new Document().append("$natural", -1));
MongoCursor<Document> cursor = it.iterator();
while(cursor.hasNext()){
Document doc = cursor.next();
System.out.println(doc.toJson());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.