简体   繁体   English

如何在mongoDB中查询存储在数组中的对象

[英]How to query for objects stored in an array in a mongoDB

I have the following nested object stored in my mongoDB: 我有以下嵌套对象存储在我的mongoDB中:

var Appointment = new Schema ({

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

I now want to query my appointments for a studentName which is stored in the array students either in user1 or user2. 我现在想查询我的约会,找到存储在用户1或用户2的数组学生中的studentName。 But I have no idea how I could achieve that? 但我不知道我怎么能做到这一点? If it is an array I would use: 如果是我将使用的数组:

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

You can use an $or operator and dot notation for this: 你可以使用$or运算符和点符号:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM