Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create mongoDB objectid in java

Refering to post How to add an array to a MongoDB document using Java? I have created a mongo schema using java it has sub elements, I am getting _id for main document I would like to get _id in sub elements also here output looks (I have marked the portion 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  "

}

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

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

like image 787
AmeyChavan Avatar asked Jun 12 '14 13:06

AmeyChavan


People also ask

How is MongoDB ObjectId created?

ObjectID is automatically generated by the database drivers, and will be assigned to the _id field of each document. ObjectID can be considered globally unique for all practical purposes. ObjectID encodes the timestamp of its creation time, which may be used for queries or to sort by creation time.

What is ObjectId in Java?

ObjectId(java.util.Date date, int machineIdentifier, short processIdentifier, int counter) Constructs a new instances using the given date, machine identifier, process identifier, and counter.

What is MongoDB ObjectId?

An ObjectID is a 12-byte Field Of BSON type. The first 4 bytes representing the Unix Timestamp of the document. The next 3 bytes are the machine Id on which the MongoDB server is running. The next 2 bytes are of process id. The last Field is 3 bytes used for increment the objectid.

What datatype is ObjectId from MongoDB?

ObjectId is one data type that is part of the BSON Specification that MongoDB uses for data storage. It is a binary representation of JSON and includes other data types beyond those defined in JSON. It is a powerful data type that is incredibly useful as a unique identifier in MongoDB Documents.


1 Answers

To create objectId programmatically, use the following syntax

import org.bson.types.ObjectId;


ObjectId id1 = new ObjectId();
ObjectId id2 = ObjectId.get();

// In case you want to mention the parent ID itself,
ObjectId id3 = new ObjectId("5399aba6e4b0ae375bfdca88");
like image 81
mohamedrias Avatar answered Sep 29 '22 16:09

mohamedrias