使用mongoose查询来自[数组对象]

时间:2016-08-26 15:28:12

标签: javascript mongoose

我有两个Mongo模式定义如下:

var userSchema = new mongoose.Schema({
    email: String,
    password: String, //hash created from password
    firstName: String,
    lastName: String,
    comment:{userComment:String,adminComment:String},
    postalAddress: String,
    city: String,
    state: String,
    country: String,
    institution: String,
    privilege: {type: String, enum:['normal','chair','admin']},
    status: {type:String, enum: ['granted','removed','pending']},
    myConference:[{type:Schema.Types.ObjectId,ref:'Conference'}],
    mySubmission:[{type:Schema.Types.ObjectId,ref:'Submission'}]
});

var conferenceSchema = new mongoose.Schema({
    conferenceTitle: {type:String},
    conferenceDescription: String,
    conferenceStartDate:{type:Date, default: Date.now},
    submissionEndDate:{type:Date},
    reviewEndDate:{type:Date},
    **conferenceMembers:[{type:Schema.Types.ObjectId,ref:'User'}]**,
    conferenceSubmissions:[{type:Schema.Types.ObjectId,ref:'Submission'}],
    createdBy:{type:Schema.Types.ObjectId,ref:'User'},
    //chairMembers:[{type:Schema.Types.ObjectId,ref:'User'}],
    department:String
});

要求:我想获取与某个_id匹配的所有会议对象,即每个用户'架构对象。 conferenceMembers是一群用户'对象

我做了什么:

这是一个POST:

var userId=req.body.userId

**Conference.find({userId: {$in: [Conference.conferenceMembers]}},function(err,conf){**
if(err){
                return res.send(500, err);
            }
return res.send(200,conf);

但是,过滤器似乎没有在这里工作,我尝试使用$ elemMatch,但没有运气。

1 个答案:

答案 0 :(得分:1)

要获取userId中具有特定conferenceMembers的所有文档,您可以执行以下操作:

Conference.find({conferenceMembers : userId}).exec(function(err,conf){...});

如果您想填充用户,也可以使用mongoose填充。

Conference.find({conferenceMembers : userId}).populate('conferenceMembers').exec(function(err,conf){...});