[英]Querying from [array of objects] with mongoose find
I have two Mongo schemas defined as follows: 我有两个定义如下的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
});
Requirement: I want to fetch all the Conference objects which match a certain _id ie unique for each 'User' schema object. 要求:我想获取所有与某个_id匹配的会议对象,即每个“用户”模式对象唯一的_id。 conferenceMembers is an array of 'User' objects
ConferenceMembers是“用户”对象的数组
What I did: 我做了什么:
It's a POST: 这是一个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);
But, the filter doesn't seem to work here, I tried with $elemMatch as well but no luck. 但是,过滤器似乎在这里不起作用,我也尝试了$ elemMatch,但是没有运气。
To fetch all the documents which has specific userId
in conferenceMembers
, you can do this: 要在
conferenceMembers
获取所有具有特定userId
的文档,您可以执行以下操作:
Conference.find({conferenceMembers : userId}).exec(function(err,conf){...});
if you want to populate the users too you can use mongoose populate. 如果您也想填充用户,则可以使用猫鼬填充。
Conference.find({conferenceMembers : userId}).populate('conferenceMembers').exec(function(err,conf){...});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.