【问题标题】:Multiple collection join/concat query with Mongoose similar to union query in SQL使用 Mongoose 进行多集合连接/连接查询,类似于 SQL 中的联合查询
【发布时间】:2021-09-23 01:01:55
【问题描述】:

我有 4 个集合,分别命名为学生、教师、员工和经理,具有相同的猫鼬模式。

const schema = new Schema({
    name: String,
    age: Number,
    email: String
})

如何在一个查询中检索年龄>=25 的所有学生、员工、经理和教师? 数据应该看起来像在联合查询中执行的 SQL 中的连接。

【问题讨论】:

    标签: mongodb mongoose mongodb-query aggregation-framework


    【解决方案1】:

    您可以使用$unionWith。在pipeline 中指定年龄 >= 25 个条件。

    这里是Mongo Playground 供您参考。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2023-02-03
      • 1970-01-01
      相关资源
      最近更新 更多