简体   繁体   English

在Java中查询集合中的最新条目

[英]Query the most recent entries in a collection in Java

This mongodb query below returns the most recent entries to a collection 下面的此mongodb查询将最新条目返回到集合中

db.RSS.find().limit(6).sort({$natural:-1}).pretty()

Does anyone know how to implement this query in Java? 有谁知道如何在Java中实现此查询?

Using Mongo-Java Driver, this code is an example: 使用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.

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