How to create mongodb objectid in Java

How to add an array to a mongodb document using Java? after

{

"_id" : ObjectId("5399aba6e4b0ae375bfdca88"),"addressDetails" : [
    {
        //   _id here
        "locationName" : "Office","phones" : [
            {   //   _id here   
                "name" : "Tel1","value" : "95253-"
            },{   //   _id here
                "name" : "Tel2",{   //   _id here
                "name" : "Tel3",{   //   _id here
                "name" : "Fax1","value" : "0253-"
            }
        ],"address" : "A-3,MIDCA-3,MIDC","defaultBillAddreRSS" : "","pincode" : "422 010","city" : null,"state" : "1","country" : ""
    },{       //   _id here
        "locationName" : "Factory","phones" : [
            {   //   _id here
                "name" : "Tel1","value" : "0253-"
            },"address" : "A-3 INDUSTRIAL AREA,","country" : ""
    }
],"crLimit" : "0.0","crPeriod" : "","name" : "CROMPTON GREAVES  "

}

The Java code to be created is similar to how to add an array to a mongodb document using Java?

Is there any code to programmatically create objectid ("") in Java?

Solution

To programmatically create an objectid, use the following syntax

ObjectId id = new ObjectId();

or

ObjectId id = ObjectId.get();

If you want to mention the parent ID itself,

then

ObjectId id = new ObjectId("5399aba6e4b0ae375bfdca88");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>