2014-06-12 8 views
6

riferendosi la postare How to add an array to a MongoDB document using Java? ho creato uno schema mongo utilizzando Java ha elementi secondari, che sto ottenendo _id per documento principale vorrei ottenere _id in elementi secondari anche qui sguardi di uscita (ho segnato la parte in cui ho bisogno _id) b.party.find().pretty();come creare MongoDB IDOggetto in java

{ 

"_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 " 

}

codice

Java per creare è simile a How to add an array to a MongoDB document using Java?

Esiste un codice per creare ObjectId("") in modo programmatico in java?

+0

l'obbiettivo è c reated quando si inserisce un oggetto nel database. – njzk2

+0

@ id njzk2 viene creato automaticamente per i documenti padre, sono interessato a ottenere id per i sotto elementi – AmeyChavan

risposta

7

Per creare objectId programmazione, utilizzare la seguente sintassi

ObjectId id = new ObjectId(); 

o

ObjectId id = ObjectId.get(); 

Nel caso in cui si desidera parlare della ID madre stessa,

poi

ObjectId id = new ObjectId("5399aba6e4b0ae375bfdca88"); 
+0

ID oggetto ID = nuovo ObjectId(); ha funzionato perfettamente – AmeyChavan