Java MongoDB FindOne 获取最后插入的记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22965106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java MongoDB FindOne to get last inserted record
提问by mikepinch
I am running a large data feed into Mongo that I need to have an external client connect to and tap into the feed from the last available record - not anything older than current. I have a tailable cursor that works just fine, but it starts at the beginning of the table, and I'd like to have it start at the most recently inserted record. I know how to do this, presuming I have the _ID of the last inserted record. My problem is that I can't get findOne working properly in Java to bring back the last inserted record. Put simply, I need the Java equivalent of this Mongo JS command:
我正在向 Mongo 运行一个大数据提要,我需要有一个外部客户端连接到并从最后一条可用记录中接入提要 - 不能比当前记录更旧。我有一个可以正常工作的可尾游标,但它从表的开头开始,我希望它从最近插入的记录开始。我知道怎么做,假设我有最后插入的记录的 _ID。我的问题是我无法让 findOne 在 Java 中正常工作以恢复最后插入的记录。简而言之,我需要这个 Mongo JS 命令的 Java 等价物:
db.market.findOne( {$query:{}, $orderby:{$natural:-1}} )
There are a couple of posts on here that I've found that seem similar, but they are assuming that the client is also the one inserting the records and already have knowledge of the last available ID.
我发现这里有几篇帖子看起来很相似,但他们假设客户端也是插入记录的人,并且已经知道最后一个可用的 ID。
Anyways, what would the proper corresponding Java code be to accomplish the same thing - that is getting the _ID of the last available record?
无论如何,要完成相同的事情,正确的相应 Java 代码是什么 - 即获取最后一条可用记录的 _ID?
I guess as an alternative I could have my client insert a throwaway record, get that ID, and start from there, but I'd prefer to do this the right way.
我想作为替代方案,我可以让我的客户插入一次性记录,获取该 ID,然后从那里开始,但我更愿意以正确的方式执行此操作。
Thanks
谢谢
采纳答案by daveh
To be clear, natural order is not insertion order, except in the case of capped collections. You will need another criteria to sort by.
需要明确的是,自然顺序不是插入顺序,除非有上限集合。您将需要另一个标准来排序。
Assuming you are using the default ObjectID, you can use this as a metric for insertion as the default value starts with the insertion time (to the millisecond) and is always unique.
假设您使用默认的 ObjectID,您可以将其用作插入的度量标准,因为默认值从插入时间开始(以毫秒为单位)并且始终是唯一的。
You should also use a find, rather than a findOne. Try the following:
您还应该使用 find,而不是 findOne。请尝试以下操作:
db.market.find({}).sort({_id:-1}).limit(1)
回答by fils capo
if you want to do it in your JAVA code you cand do like this
如果你想在你的 JAVA 代码中做到这一点,你可以这样做
Document myDoc = (Document)collection.find().sort(new BasicDBObject(<field>,-1)).first();
it will return a document the is the last inserted ordered by that significant field =)
它将返回一个文档,该文档是由该重要字段排序的最后一个插入的文档 =)
回答by Alex Gordienko
For anyone who is looking for a more recent answer:
对于正在寻找更新答案的任何人:
Here's how you finding the last inserted element by "_id" with mongodb-driver in your Java project.
以下是在 Java 项目中使用 mongodb-driver 通过“_id”查找最后插入的元素的方法。
// connect to your collection.
MongoCollection<Document> collection = database.getCollection("yourCollection");
//request last inserted doc
Document myDoc = collection.find().sort(new Document("_id", -1)).first();
Hope this helps!
希望这可以帮助!
回答by Amal lal T L
Document lastInsertion = new MongoClient().getDatabase("collectionName").find().sort(new BasicDBObject("_id", -1)).first();
lastInsertion.get("key");