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

emeijp43  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(58)

**已关闭。**此问题需要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“条件连接”语法来强制“完全连接”,这里有一个快速示例:

db.EngineeringStudent.aggregate([
  {
    $facet: {
      EngineeringStudent: [
        {
          $match: {}
        }
      ],
      ScienceStudent: [
        {
          $limit: 1
        },
        {
          $lookup: {
            from: "ScienceStudent",
            pipeline: [],
            as: "ScienceStudent"
          }
        },
        {
          $unwind: "$ScienceStudent"
        },
        {
          $replaceRoot: {
            newRoot: "$ScienceStudent"
          }
        }
      ]
    }
  },
  {
    $project: {
      users: {
        "$concatArrays": [
          "$EngineeringStudent",
          "$ScienceStudent"
        ]
      }
    }
  },
  {
    $unwind: "$users"
  },
  {
    $replaceRoot: {
      newRoot: "$users"
    }
  }
])

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

{
      "studentId",
      "name",
      ...
     "department": "engineer" | "Science" ...
}

相关问题