mongoDB 获取最后插入的文档的ObjectID

    xiaoxiao2021-03-25  247

    http://stackoverflow.com/questions/3338999/get-id-of-last-inserted-document-in-a-mongodb-w-java-driver mongoDB api就可以实现,请仔细查看集合insert方法的源代码     a.文档插入后可以获取到插入的文档的ObjectID 代码: BasicDBObject doc = new BasicDBObject( "name", "Matt" ); collection.insert( doc ); ObjectId id = (ObjectId)doc.get( "_id" );   b.文档插入之前,可以自己生成一个ObjectID,简单的方法如下 代码: //It‘s safe to do doc.set("_id", new ObjectId()) //if you look at driver code if ( ensureID && id == null ){     id = ObjectId.get();     jo.put( "_id" , id );       } public static ObjectId get(){     return new ObjectId(); }       参看文章链接: http://www.thebirdietoldme.com/userActions/thread/Question.aspx?id=3338999 http://stackoverflow.com/questions/7720256/mongodb-getting-objectid-of-last-inserted-document-with-multiple-concurrent-wr

    mongoDB 获取最后插入的文档的ObjectID/_id方法

    标签:style   代码   java   com   http   方法   Go   si   it   

    原文:http://www.cnblogs.com/svennee/p/4080795.html

    欢迎访问我的技术群425783133

    转载请注明原文地址: https://ju.6miu.com/read-151.html

    最新回复(0)