如何在java中创建mongoDB objectid [英] how to create mongoDB objectid in java
问题描述
参考帖子如何添加一个使用 Java 将数组转换为 MongoDB 文档?我已经使用 java 创建了一个 mongo 模式它有子元素,我正在获取主文档的 _id
我想在子元素中获得 _id
也在这里输出看起来(我已经标记了我需要 _id
的部分)b.party.find().pretty();
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 代码类似于 如何使用 Java 向 MongoDB 文档添加数组?
Java code to create is similar to How to add an array to a MongoDB document using Java?
是否有任何代码可以在java中以编程方式创建ObjectId("")
?
Is there any code to create ObjectId("")
programmatically in java?
推荐答案
要以编程方式创建 objectId,请使用以下语法
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");
这篇关于如何在java中创建mongoDB objectid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!