获取带有Java驱动程序的mongoDB中最后插入的文档的ID [英] Get ID of last inserted document in a mongoDB w/ Java driver
本文介绍了获取带有Java驱动程序的mongoDB中最后插入的文档的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种简单的方法可以使用Java驱动程序获取mongoDB实例的最后插入文档的ID(ObjectID)?
Is there an easy way to get the ID (ObjectID) of the last inserted document of a mongoDB instance using the Java driver?
推荐答案
讨厌回答我自己的问题,但我刚才意识到你可以这样做:
Hate to answer my own question, but I just realized you can do this:
BasicDBObject doc = new BasicDBObject( "name", "Matt" );
collection.insert( doc );
ObjectId id = (ObjectId)doc.get( "_id" );
这篇关于获取带有Java驱动程序的mongoDB中最后插入的文档的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文