Spring Boot 在AWS DocumentDB中创建包含两个集合的mongoDB视图[已关闭]

emeijp43  于 2024-01-06  发布在  Spring
关注(0)|答案(1)|浏览(147)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
9天前关闭
这篇文章是编辑并提交审查8天前.
Improve this question
我有两个集合,很少有属性是共同的。
例如:
工程系学生

  • 学生证
  • 名称
  • 卷筒编号
  • 出生日期
  • 入学年份

科学学生

  • 学生证
  • 名称
  • 卷筒编号
  • 出生日期
  • 入学年份

两个集合都有自己的属性。我想创建一个名为Student的视图,该视图将包含两个学生。
MongoDB支持视图,但DocumentDB不支持视图。
使用unionWith进行聚合:documentDB也不支持此操作。
我们有没有其他方法来做类似的功能。$lookup是做左外连接,但对我来说,它是联合比加入。
$lookup会导致很多性能问题。
有没有其他方法可以用一个查询从两个集合中进行查询

mbjcgjjk

mbjcgjjk1#

您可以使用$lookup“条件连接”语法来强制“完全连接”,这里有一个快速示例:

  1. db.EngineeringStudent.aggregate([
  2. {
  3. $facet: {
  4. EngineeringStudent: [
  5. {
  6. $match: {}
  7. }
  8. ],
  9. ScienceStudent: [
  10. {
  11. $limit: 1
  12. },
  13. {
  14. $lookup: {
  15. from: "ScienceStudent",
  16. pipeline: [],
  17. as: "ScienceStudent"
  18. }
  19. },
  20. {
  21. $unwind: "$ScienceStudent"
  22. },
  23. {
  24. $replaceRoot: {
  25. newRoot: "$ScienceStudent"
  26. }
  27. }
  28. ]
  29. }
  30. },
  31. {
  32. $project: {
  33. users: {
  34. "$concatArrays": [
  35. "$EngineeringStudent",
  36. "$ScienceStudent"
  37. ]
  38. }
  39. }
  40. },
  41. {
  42. $unwind: "$users"
  43. },
  44. {
  45. $replaceRoot: {
  46. newRoot: "$users"
  47. }
  48. }
  49. ])

字符串
Mongo Playground
这样做的缺点是需要源集合(在我的示例中,“EngineeringStudent”集合至少有1个文档)。
在不了解您的应用程序和需求的情况下,让我简单地提出一个建议,与其复制具有相同结构的多个集合,为什么不将它们统一到一个具有department字段的模式中呢?
然后,您可以将所有用户放在同一个集合中,同时还允许您根据他们的部门查询子集。

  1. {
  2. "studentId",
  3. "name",
  4. ...
  5. "department": "engineer" | "Science" ...
  6. }

展开查看全部

相关问题