英文:
MongoDB Java driver converts id field of nested object into _id
问题
以下是翻译好的内容:
我有一个嵌套对象,其中定义了一个名为 id
的字段,但在持久化到 mongodb 时会转换为 _id
。我正在尝试避免这种情况,因为嵌套对象的 id 字段不是一个 ObjectId。
这里有两个简单的 POJO 实体:
Tenant.java
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Tenant {
private ObjectId id;
private AppClient appClient;
private Date createdAt;
}
AppClient.java
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class AppClient {
private String id;
private String secret;
private String role;
}
这是我正在使用 mongodb java 驱动程序创建租户并持久化的示例方式。
public class MainTest {
public static void main(String[] args) {
ConnectionString connString = new ConnectionString("mongodb://127.0.0.1:27017");
CodecRegistry pojoCodecRegistry = fromProviders(PojoCodecProvider.builder().automatic(true).build());
CodecRegistry codecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), pojoCodecRegistry);
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(connString)
.codecRegistry(codecRegistry)
.build();
MongoClient mongoClient = MongoClients.create(settings);
MongoCollection<Tenant> mongoCollection = mongoClient.getDatabase("alpha").getCollection("tenant", Tenant.class);
Tenant tenant = new Tenant();
AppClient appClient = new AppClient();
appClient.setId("12312313");
tenant.setAppClient(appClient);
mongoCollection.insertOne(tenant);
Tenant findResult = mongoCollection.find(new Document().append("appClient.id", "12312313")).first();
}
由于这个插入操作,appClient 的字段 _id
会代替 id
。
{ "_id" : ObjectId("5f4a80b4a0bf93152208e210"), "appClient" : { "_id" : "12312313" } }
这在按 id 搜索内部文档时会引发问题。因此,在上面的示例中,findResult 将为 null,因为没有定义任何 id
字段。感谢您的帮助。无论如何,谢谢!
英文:
I have a nested object with an id
field defined which is converted as _id
when persisting into mongodb. I am trying to avoid this since the nested object's id field is not an ObjectId.
Here are two simple POJO entities I have;
Tenant.java
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Tenant {
private ObjectId id;
private AppClient appClient;
private Date createdAt;
}
AppClient.java
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class AppClient {
private String id;
private String secret;
private String role;
}
And this is the example way I am creating a tenant and persisting with mongodb java driver.
public class MainTest {
public static void main(String[] args) {
ConnectionString connString = new ConnectionString("mongodb://127.0.0.1:27017");
CodecRegistry pojoCodecRegistry = fromProviders(PojoCodecProvider.builder().automatic(true).build());
CodecRegistry codecRegistry = fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), pojoCodecRegistry);
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(connString)
.codecRegistry(codecRegistry)
.build();
MongoClient mongoClient = MongoClients.create(settings);
MongoCollection<Tenant> mongoCollection = mongoClient.getDatabase("alpha").getCollection("tenant", Tenant.class);
Tenant tenant = new Tenant();
AppClient appClient = new AppClient();
appClient.setId("12312313");
tenant.setAppClient(appClient);
mongoCollection.insertOne(tenant);
Tenant findResult = mongoCollection.find(new Document().append("appClient.id", "12312313")).first();
}
As a result of this insert appClient has _id field instead of id.
{ "_id" : ObjectId("5f4a80b4a0bf93152208e210"), "appClient" : { "_id" : "12312313" } }
This causing an issue when searching inner documents by id. So, in the above example findResult will be null since there is no any id
field defined. I appreciate for your help. Thanks anyway!
答案1
得分: 0
只是发现 MongoDB Java 驱动程序将每个以 id
命名的字段默认更改为 _id
。因此,解决方法是更改 ANNOTATION_CONVENTION
的默认约定类型,如下所示:
PojoCodecProvider.builder().conventions(asList(Conventions.ANNOTATION_CONVENTION)).automatic(true).build();
此外,我们必须使用 BsonId
注释 id
字段,该字段是一个 ObjectId。我们的 Tenant POJO 将如下所示:
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Tenant {
@BsonId
private ObjectId id;
private AppClient appClient;
private Date createdAt;
}
相关行为在 https://jira.mongodb.org/browse/JAVA-2750 中有解释。
英文:
Just found that mongodb java driver defaults every field named with id
to _id
. So, the workaround was changing the default convention type of ANNOTATION_CONVENTION
as shown below;
PojoCodecProvider.builder().conventions(asList(Conventions.ANNOTATION_CONVENTION)).automatic(true).build();
Additionally, we have to annotate id field which is an ObjectId with BsonId. Our Tenant POJO will looke like;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Tenant {
@BsonId
private ObjectId id;
private AppClient appClient;
private Date createdAt;
}
The related behavior is explained at https://jira.mongodb.org/browse/JAVA-2750
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论