0

ユーザーが指定された 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 クエリはここでは何もしません...

4

1 に答える 1

0

現在、データベースではなくプログラムで比較を行っています。

User.findById(userId)
.populate('roles.role')
.exec(function(err, data){
    if(!err){
        if(data){
            if(data.roles.find(function(element, index, array){
                return element.project == projectId && element.role.name == roleName;
            }))
                return callback(true);
        }
    }
    return callback(false);
});
于 2015-12-05T15:12:59.230 に答える