如何在java中创建mongoDB objectid

前端之家收集整理的这篇文章主要介绍了如何在java中创建mongoDB objectid前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参考 How to add an array to a MongoDB document using Java?
我用java创建了一个mongo模式
它有子元素,我得到主文件的_id
我想在子元素中得到_id这里输出看起来(我已经标记了我需要的部分_id)b.party.find().pretty();
{

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

}

要创建的Java代码类似于How to add an array to a MongoDB document using Java?

是否有任何代码在java中以编程方式创建ObjectId(“”)?

解决方法

要以编程方式创建objectId,请使用以下语法
ObjectId id = new ObjectId();

要么

ObjectId id = ObjectId.get();

如果你想提到父ID本身,

然后

ObjectId id = new ObjectId("5399aba6e4b0ae375bfdca88");
原文链接:https://www.f2er.com/java/121074.html

猜你在找的Java相关文章