ユーザーが指定された ID、指定された ID を持つプロジェクト、および指定された名前を持つロールを持っているかどうかをテストする必要があります。
var UserSchema = new Schema({
roles: [{
project: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Project',
},
role: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Role',
}
}]
});
と
var RoleSchema = new Schema({
name: {
type: String
}
});
.populate してから .where を適用しようとしましたが、.where は何もしません。
populate の後に .and を使用しても機能しません。
mongodb/mongooseでこれを解決するには?
ありがとうございました!
//EDIT今、私はそのようなものを持っていますが、それは機能しません(.whereは何もしません)そしてそれは本当に美しくありません:
User.findById(userId)
.populate({
path: 'roles.role',
match: { 'name': roleName}
})
.where('roles.project').equals(projectId)
.exec(function(err, data){
data.roles = data.roles.filter(function(f){
return f.role;
})
if(!err){
if(data){
if(data.roles.length == 1) {
return true;
}
}
}
return false;
});
ケビン B が言ったことを実行すると、次のようになります。
Role.findOne({name: roleName}, function(err, data){
if(!err){
if(data){
User.findById(userId)
.and([
{'roles.project': projectId},
{'roles.role': data._id}
])
.exec(function(err2, data2){
if(!err2){
if(data2){
console.log(data2);
}
}
});
}
}
});
.and クエリはここでは何もしません...