【发布时间】:2017-06-09 07:57:42
【问题描述】:
我有这个猫鼬模型:
var user = new Schema({
email : {type: String},
firstName: {type: String},
lastName: {type: String},
username: {type:String},
password: {type:String},
privacy : {
displayEmail: {type: Boolean, default: true},
displayUsername: {type:Boolean: default: true},
displayfirstName: {type:Boolean: default: true},
displaylastName: {type:Boolean: default: true}
})
假设用户将displayEmail abd displayLastName 的值设置为false。
然后,我有一个简单的 GET 请求,它返回一个包含所有用户详细信息的 json 对象。如何查询 mongo 以仅返回隐私对象中具有 true 的字段?如果displayfirstName 为真,则应返回firstName 值。
更新
var user = new Schema({
email : {type: String},
firstName : {type: String},
lastName : {type: String},
username : {type:String},
password : {type:String},
privacy : {
displayEmail : {type: Boolean, default: true},
displayUsername : {type:Boolean, default: true},
displayfirstName : {type:Boolean, default: true},
displaylastName : {type:Boolean, default: true},
displayAddress : {type: Boolean, default: true}, // new
displayPhone : {type: Boolean, default: true} // new
},
extraInfo : {
userAddress : {type:String}, // new
userPhone : {type: String} // new
}
})
我如何有条件地检查地址和电话字段?
【问题讨论】:
标签: node.js mongodb express mongoose mongodb-query