0
我需要測試用戶是否具有給定的id,具有指定id的項目和具有給定名稱的角色。mongoose query .where/.and after .populate
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
}
});
我tryed到.populate,然後申請。哪裏,但是。凡不執行任何操作。
使用.and填充後也不起作用。
如何在mongodb/mongoose中解決這個問題?
謝謝!
//編輯 現在我有類似的東西,不工作(。凡不執行任何操作),它是真的不漂亮:
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;
});
當我做什麼凱文乙說:
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);
}
}
});
}
}
});
的。而查詢少了點什麼這裏...
向我們顯示您的查詢...甚至不工作的代碼在這裏是有價值的。 –
在這種情況下,您首先必須查詢具有給定名稱的角色的角色集合,然後您必須通過id查詢具有角色和項目的用戶。 –