大家好,我在仅从数据库中获取用户允许的字段时遇到问题。 所以我的架构是:
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/