MongoDB gets the objectid/_id method for the last inserted document

Source: Internet
Author: User

Http://stackoverflow.com/questions/3338999/get-id-of-last-inserted-document-in-a-mongodb-w-java-driver
MongoDB API can be implemented, please carefully review the source code of the collection Insert method A. After inserting a document, you can get the Objectid of the inserted document .Code:Basicdbobject doc = new Basicdbobject ("name", "Matt");
Collection.insert (DOC);
ObjectId id = (ObjectId) doc.get ("_id"); B. Before the document is inserted, you can generate a objectid yourself, the simple way is as followsCode://it ' s safe to do

doc.set ("_id", New ObjectId ())
//if at driver code

if (ensureid && id = = NULL) {
id = objectid.get ();
jo.put ("_id", id);
}

Public static ObjectId get () {
return new ObjectId ();
}See article Link: http://www.thebirdietoldme.com/userActions/thread/Question.aspx?id=3338999http://stackoverflow.com/ Questions/7720256/mongodb-getting-objectid-of-last-inserted-document-with-multiple-concurrent-wr

MongoDB gets the objectid/_id method for the last inserted document

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.