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查询具有角色和项目的用户。 –