这个问题的标题很矛盾,因为我试图在非关系数据库中实现关系…:)

但我的意思是,如何在使用MongoDB的应用程序模型类中使用define associations between entities.

在使用JPA时,我经常使用@ManyToMany@OneToMany个注释来定义对象之间的关系.Spring Data MongoDB中有类似的东西吗?

在研究MongoDB时,我意识到有两种可能的联系方式:ReferencesEmbedded Data.

Spring数据使用哪一个?是否可以配置关联模式?

推荐答案

可以使用@DBRef注释将引用的类持久化到单独的集合中,否则该文档将持久化到同一个文档(json)中.

spring data documentation

@DBRef - applied at the field to indicate it is to be stored using a com.mongodb.DBRef.

7.3.4 Using DBRefs The mapping framework doesn't have to store child objects embedded within the document. You can also store them separately and use a DBRef to refer to that document. When the object is loaded from MongoDB, those references will be eagerly resolved and you will get back a mapped object that looks the same as if it had been stored embedded within your master document.

Mongodb相关问答推荐

MongoDB 4.4如何使用未知密钥更新dict

使用MongoDB 4将根文档替换为数组

MongoDB获取所有文档谁的S子文档只包含一个特定值?

发布到 MongoDB 时生成的附加id字段

查询有关 Go 项目中对象数组的 MongoDb 集合

如何将交易列表变成 token 数量的对象?

$group 和 sum + 添加所有大于

映射数组导致 mongodb 聚合

Stripe:必须提供来源或客户

从性能Angular 来看 MongoDB 嵌入式与参考

.NET 中的 Mongodb 单元测试

Mongoose 的保存回调是如何工作的?

嵌套数组内的Mongodb增量值

无法从 MongoDb C# 中的 BsonType ObjectId 反序列化字符串

MongoDB - 我如何找到另一个集合中的文档未引用的所有文档

在MongoDB中查询一个半​​径内的位置

Mongoose / MongoDB 用户通知方案建议

Mongodb错误:The positional operator did not find the match needed from the query

mongo dbname --eval 'db.collection.find()' 不起作用

mongoose中的 Date.now() 和 Date.now 有什么区别?