@MongoId 在 Spring Data MongoDB 中通过 @Id 的用途是什么? [英] What is use of @MongoId in Spring Data MongoDB over @Id?

查看:33
本文介绍了@MongoId 在 Spring Data MongoDB 中通过 @Id 的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将模型添加到 Mongo Db.

I am trying to add Model to Mongo Db.

我知道我可以使用@Id 来避免在 _id 字段中创建 ObjectId.现在,在使用它时,我从 org.springframework.data.mongodb.core.mapping 包中遇到了 @MongoId.

I know I can use @Id to avoid ObjectId creation in _id field. Now while using this I came across @MongoId from org.springframework.data.mongodb.core.mapping package.

所以我的问题是我们可以在@Id 上使用@MongoId,如果是,用例是什么?不幸的是,@MongoId

So my question is can we use @MongoId over @Id, if yes, whats use case? Unfortunately not enough documentation available for @MongoId

类似于以下内容:

import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.MongoId;

import java.util.Date;

@Document
public class LeadDetails {
    @MongoId
    private Integer id;
    private String firstName;
    private String middleName;
    private String lastName;
    private String mobile;
    private String landlineNumber;
    private Date contactedOn;
    private Date repliedOn;
    private String email;
}

推荐答案

某些环境需要一种自定义方法来映射 Id 值,例如存储在 MongoDB 中的未通过 Spring Data 映射层运行的数据.文档可以包含可以表示为 ObjectId 或 String 的 _id 值.从存储读取文档回域类型工作正常.由于隐式 ObjectId 转换,通过 id 查询文档可能很麻烦.因此无法以这种方式检索文档.对于这些情况,@MongoId 提供对实际 id 映射尝试的更多控制.

Some environments require a customized approach to map Id values such as data stored in MongoDB that did not run through the Spring Data mapping layer. Documents can contain _id values that can be represented either as ObjectId or as String. Reading documents from the store back to the domain type works just fine. Querying for documents via their id can be cumbersome due to the implicit ObjectId conversion. Therefore documents cannot be retrieved that way. For those cases @MongoId provides more control over the actual id mapping attempts.

这篇关于@MongoId 在 Spring Data MongoDB 中通过 @Id 的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆