Spring Data MongoDB中的手动引用与DBRef?

问题描述 投票:0回答:1

我仍在学习MongoDB及其驱动程序,请原谅我的无知。

我知道Spring Data MongoDB可以自动映射如下内容:

{
  "_id" : ObjectId("5cd4e8140615c7480f549907"),
  "name" : "test",
  "otherCollection" : [ 
    {
      "$ref" : "otherCollection",
      "$id" : ObjectId("5cd4e8140615c7480f549905")
    }
  ]
}

它在嵌套集合中完美地填充了我的POJO。但是我也尝试了手动引用,如下所示:

{
  "_id" : ObjectId("5cd4e8140615c7480f549906"),
  "name" : "test",
  "otherCollection" : [ 
    ObjectId("5cd4e8140615c7480f549905")
  ]
}

这是我收到的错误

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.bson.types.ObjectId] to type [com.test.test.model.OtherObject]

在手册中的文档中说

Then your application can run a second query to return the related data.

现在,我了解到它们从字面上意味着您自己必须加入并将它们加入代码中。现在,对于DBRef,他们有这个

Some community supported drivers may have alternate behavior and may resolve a DBRef into a document automatically.

我的问题:是许多驱动程序(如Spring Data MongoDB)之所以为您解析DBRef,是因为它们的作用域是单个集合?而且没有手动参考自动解析之类的东西,因为他们将不得不扫描整个数据库?

以及后续工作,我的模型包含有限的多对多关系集,其中也包含大量重复数据,所以我不想嵌入。如果对象数组中的父子对象被翻转,则需要该用例。

我的选择也是

  1. 编写一个自定义类型转换器以在Spring Data MongoDB中进行手动引用(甚至是吗?)>>
  2. 对集合中的所有项目使用DBRefs
  3. 具有一个单独的关系表,该关系表链接ID并使用$lookup查询它们(性能与DBRef相比如何?
  4. 不将MongoDB用于许多多对多用例吗?也许这是错误的工具,我应该坚持使用RDBMS数据库?
  5. 非常感谢。

我仍在学习MongoDB及其驱动程序,请原谅我的无知。我了解到Spring Data MongoDB可以自动映射如下内容:{“ _id”:ObjectId(“ 5cd4e8140615c7480f549907”)...

mongodb nosql spring-data-mongodb
1个回答
0
投票

看看relmongo

© www.soinside.com 2019 - 2024. All rights reserved.