我有一个密码和盐存储在mongodb,我可以查询除盐以外的所有其他字段 .
这是架构:
var userSchema = new Schema({
firstname : { type: String, trim: true},
lastname : { type: String, trim: true},
email : { type: String, required: true, unique: true, trim:true},
password : { type: String, required: true},
salt : { type: String, required: true, trim: true, unique: true},
addedOn : { type: String, required: false}
});
...
这里有一份doc的请求:
User.findOne({
email: req.body.email
}).exec(function(err, obj) {
if (err) {
console.log("MongoDB Error: " + err);
return res.status(500); // or callback
}
else if (obj) {
console.log(obj.firstname); //This works
console.log(obj.salt); // And this doesn't
}
});
但是当我从终端运行db.users.find()时,这个doc存在并且有一个salt .
编辑:
我在这里找到了我的问题的解决方案How to protect the password field in Mongoose/MongoDB so it won't return in a query when I populate collections?
2 回答
使用此方法,您可以强制mongoose查询并返回mongodb集合中的某个字段:
我喜欢Mital的答案,它检索字段但由于某种原因返回的值不可用 .
这相当有效 .