2016-08-14 141 views
1

大家好我有一个问题,从数据库只允许由用户获取字段。 所以我的模式是:根据权限字段选择字段

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'}] 
}); 

,我试图让仅在许可领域拥有真正的,这意味着它允许领域。所以我正在运行以下查询:

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

如何仅选择允许的字段?

回答

1

试试这个,这,可能会得到你想要的东西:

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); 
}); 
1

你可以做一个查询与链接,因为文件从查询启用了精益选项返回lean()方法是纯JavaScript对象,不MongooseDocuments和操纵通过由权限决定除去键返回的对象领域的物体:

Object.filter = function(obj, predicate) { 
    var result = {}, key; 
    for (key in obj) { 
     if (obj.hasOwnProperty(key) && !predicate(obj[key])) { 
      result[key] = obj[key]; 
     } 
    } 
    return result; 
}; 

database.Profile.findOne({_id: req.params.id}).lean().exec(function (err, doc) { 
    if (err) return handleError(err); 
    console.log(doc); 
    console.log(doc.permissions); 
    var filtered = Object.filter(doc.permissions, 
     function (key){ return doc.permissions[key]; } 
    ); 
    console.log(filtered); 
    res.send(filtered); 
}); 

使用猫鼬投影select()方法的另一种替代方法是做两个查询;第一个将返回整个文档,并在未来将查询相同的文档,但基于项目的权限对象的字段的字段:

下面显示了这一点:

database.Profile.findOne({_id: req.params.id}).lean().exec(function (err, doc) { 
    console.log(doc); 
    console.log(doc.permissions); 
    var projection = Object.keys(doc.permissions) 
          .filter(function (key){ return doc.permissions[key]; }) 
          .join(' '); 
    console.log(projection); 
    database.Profile.findOne({_id: req.params.id}) 
     .select(projection) 
     .exec(function (err, profile) { 
      if (err) return handleError(err); 
      res.send(profile); 
     });  
}); 
+1

我的第二个解决方案解决它,你在我发布这个问题后提出了这个问题,但是这个需要两个查询。我没有检查第一个问题,因为'ravi shankar'的第一个答案很好,谢谢。 –

+0

@AlexisPavlidis不用担心,如果您事先不知道权限密钥,则不是最佳解决方案之一,而只是一种替代方案。总的来说,我认为拉维的回答就足够了,因此+1。 – chridam