【问题标题】:How to query for objects stored in an array in a mongoDB如何查询存储在 mongoDB 数组中的对象
【发布时间】:2013-08-26 21:06:58
【问题描述】:

我的 mongoDB 中存储了以下嵌套对象:

var Appointment = new Schema ({

    students: [{user1:String,user2:String, _id: false}],
});

我现在想在我的约会中查询一个studentName,它存储在user1 或user2 的数组students 中。但我不知道我怎么能做到这一点? 如果它是一个数组,我会使用:

    Appointment.find({
        students: {$in: [studentName]}
    }, function(err, appointmentsDb) {
        // do something
    });

【问题讨论】:

    标签: javascript mongodb mongoose


    【解决方案1】:

    您可以为此使用$or 运算符和点符号:

    Appointment.find({ $or: [
        { 'students.user1': studentName },
        { 'students.user2': studentName }
    ]}, callback);
    

    【讨论】:

      猜你喜欢
      • 2022-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      相关资源
      最近更新 更多