【问题标题】:About list query of mongoose or mongoDB关于mongoose或mongoDB的列表查询
【发布时间】:2014-12-13 19:42:43
【问题描述】:

我有一个Student collection和一个Class collection,class collection有一个studentRefs,是一个学生列表 ids,但学生没有引用 班级 id 的属性。

现在,我有另一个学生 ID 列表,我想获取他们的课程。我想知道如何查询 mongoose 或 mongoDB 中的类。 mongoose 中的架构如下所示。

classSchema = Schema({
    className: String,
    studentRefs: [{type: Schema.Types.ObjectId, ref: 'students'}]
});
studentSchema = Schema({
    studentName: String
});

例如,

学生ABClass1,
学生CDClass2,
学生EFClass3
现在,我有一个[A, B, C]

我想获取该数组中学生的班级。
所以,我的预期结果是[Class1, Class2]

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您也可以将$in 运算符与studentRefs 等数组字段一起使用。因此,假设 studentIds 包含学生 ID 列表以查找课程:

    Class.find({studentRefs: {$in: studentIds}}, function(err, classes) { ... });
    

    【讨论】:

      猜你喜欢
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-01
      • 2019-03-20
      • 2019-08-24
      • 2019-01-22
      相关资源
      最近更新 更多