How to create mongoDB objectid in java

Referring to post How to add an array to a MongoDB document using Java? I created a mongo scheme, using java it has auxiliary elements, I get _id for the main document I would like to get _id in subelements and here the output looks (I noted the part where I need _id ) b.party.find().pretty();

 { "_id" : ObjectId("5399aba6e4b0ae375bfdca88"), "addressDetails" : [ { // _id here "locationName" : "Office", "phones" : [ { // _id here "name" : "Tel1", "value" : "95253-" }, { // _id here "name" : "Tel2", "value" : "95253-" }, { // _id here "name" : "Tel3", "value" : "95253-" }, { // _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-" }, { // _id here "name" : "Tel2", "value" : "0253-" }, { // _id here "name" : "Tel3", "value" : "0253-" }, { // _id here "name" : "Fax1", "value" : "0253-" } ], "address" : "A-3 INDUSTRIAL AREA,", "defaultBillAddrerss" : "", "pincode" : "422 010", "city" : null, "state" : "1", "country" : "" } ], "crLimit" : "0.0", "crPeriod" : "", "name" : "CROMPTON GREAVES " 

}

The Java code to create is similar. How to add an array to a MongoDB document using Java?

Is there any code to create an ObjectId("") programmatically in java?

+6
source share
1 answer

To create an objectId programmatically, use the following syntax

 ObjectId id = new ObjectId(); 

or

 ObjectId id = ObjectId.get(); 

If you want to specify the parent identifier itself,

then

 ObjectId id = new ObjectId("5399aba6e4b0ae375bfdca88"); 
+7
source

Source: https://habr.com/ru/post/970754/


All Articles