Javascript 如何查询mongoDB中数组中存储的对象
我的mongoDB中存储了以下嵌套对象:Javascript 如何查询mongoDB中数组中存储的对象,javascript,mongodb,mongoose,Javascript,Mongodb,Mongoose,我的mongoDB中存储了以下嵌套对象: var Appointment = new Schema ({ students: [{user1:String,user2:String, _id: false}], }); 现在我想查询一个studentName的预约,该studentName存储在数组students user1或user2中。但我不知道我如何才能做到这一点? 如果它是一个数组,我将使用: Appointment.find({ students:
var Appointment = new Schema ({
students: [{user1:String,user2:String, _id: false}],
});
现在我想查询一个studentName的预约,该studentName存储在数组students user1或user2中。但我不知道我如何才能做到这一点?
如果它是一个数组,我将使用:
Appointment.find({
students: {$in: [studentName]}
}, function(err, appointmentsDb) {
// do something
});
您可以为此使用运算符和点表示法:
Appointment.find({ $or: [
{ 'students.user1': studentName },
{ 'students.user2': studentName }
]}, callback);