【问题标题】:Manual Referencing vs. DBRef in Spring Data MongoDB?Spring Data MongoDB中的手动引用与DBRef?
【发布时间】:2019-05-10 13:48:23
【问题描述】:

我还在学习 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)为您解析 DBRefs 的原因是因为它们的范围仅限于单个集合吗?而且没有手动参考自动解析之类的东西,因为他们必须扫描整个数据库?

接下来,我的模型包含一组有限的多对多关系,其中也有很多重复数据,所以我不想嵌入。如果对象数组中的父子对象被翻转,则需要该用例。

我的选择也是

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

非常感谢您。

【问题讨论】:

    标签: mongodb nosql spring-data-mongodb


    【解决方案1】:

    看看 relmongo 框架,它允许您使用注释来实现关系。

    【讨论】:

    • 这可能是对原始问题的评论。
    猜你喜欢
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    相关资源
    最近更新 更多