如何在java中创建mongoDB objectid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24186263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:40:50  来源:igfitidea点击:

how to create mongoDB objectid in java

javamongodbmongodb-java

提问by AmeyChavan

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 _idfor main document I would like to get _idin sub elements also here output looks (I have marked the portion where I need _id) b.party.find().pretty();

参考帖子如何使用 Java 将数组添加到 MongoDB 文档?我已经使用 java 创建了一个 mongo 模式,它有子元素,我正在获取_id主文档我想_id在子元素中也在这里输出看起来(我已经标记了我需要的部分_idb.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?

要创建的 Java 代码类似于 如何使用 Java 将数组添加到 MongoDB 文档?

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

是否有任何代码可以ObjectId("")在 Java 中以编程方式创建?

采纳答案by mohamedrias

To create objectId programmatically, use the following syntax

要以编程方式创建 objectId,请使用以下语法

ObjectId id = new ObjectId();

or

或者

ObjectId id = ObjectId.get();

In case you want to mention the parent ID itself,

如果您想提及父 ID 本身,

then

然后

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

回答by john felix

To create objectId programmatically, use the following syntax

要以编程方式创建 objectId,请使用以下语法

Map<String,String> objectId = new HashMap<String,String>();
objectId.put("$oid","5399aba6e4b0ae375bfdca88");

Then insert into mongodb.

然后插入到mongodb中。