mongodb - 根据权限字段选择字段

标签 mongodb mongoose database

大家好,我在仅从数据库中获取用户允许的字段时遇到问题。 所以我的架构是:

var profileSchema = mongoose.Schema({
    authId: {type: Schema.Types.ObjectId, ref: 'Auth'},
    fname: String,
    lname: String,
    am: Number,
    email: String,
    location: String,
    languages: [String],
    birth_date: {
        type: Date,
        default: Date.now
    },
    reg_date: {
        type: Date,
        default: Date.now
    },
    last_log: {
        type: Date,
        default: Date.now
    },
    permissions: {
        location: {type: Boolean,Default:true},
        birth_date: {type: Boolean,Default:true},
        email: {type: Boolean,Default:true},
        am: {type: Boolean,Default:true},
        subjects: {type: Boolean,Default:true},
        files: {type: Boolean,Default:true},
        posts: {type: Boolean,Default:true}
    },
    ip: String,
    subjects: [{type: Schema.Types.ObjectId, ref: 'Subject'}],
    files: [{type: Schema.Types.ObjectId, ref: 'FileIndex'}],
    posts: [{type: Schema.Types.ObjectId, ref: 'Post'}],
    notifications: [{type: Schema.Types.ObjectId, ref: 'Notifications'}]
});

而且我试图只获取权限字段中具有 true 的字段,这意味着它是允许的。所以我正在运行以下查询:

database.Profile.findOne({_id: req.params.id}).exec(function (err, profile) { 
    console.log(profile);
    res.send(profile);
});

如何只选择允许的字段?

最佳答案

试试这个,它可能会让你得到你想要的:

database.Profile.findOne({_id: req.params.id},{location:$.permissions.location , birth_date:$.permissions.birth_date, email:$.permissions.email, am:$.permissions.am, subjects:$.permissions.subjects, files:$.permissions.files, posts:$.permissions.posts}).exec(function (err, profile) { 
    console.log(profile);
    res.send(profile);
});

关于mongodb - 根据权限字段选择字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38944429/

相关文章:

python - Mongoengine update_one+upsert 与不推荐使用的 get_or_create

java - Spring-Data for MongoDB 异常,同时使用@DBRef 注释进行引用

php - 将所有 mysql 错误记录到数据库或排序?

MySQL 每天计数类型

javascript - AJAX 调用到/从 Node/Express 的 MongoDB 示例?

javascript - meteor 错误 : A method named insert is already defined

node.js - $lookup 查找子数组中的每个元素

javascript - Node.js:如何获取 Error 对象中不正确的字段

javascript - Mongoose,如果从查询返回的文档字段数组中存在 ObjectId,则填充 bool 变量

sql - 如何在 SQL Server 查询游标上绑定(bind)参数