【问题标题】:How to return specific object values related to privacy values?如何返回与隐私值相关的特定对象值?
【发布时间】: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


    【解决方案1】:

    我能想到的唯一方法是通过一个聚合管道,它有一个 $project 运算符和一个评估条件并返回值的 $cond 表达式其他两个表达式之一取决于评估的逻辑。

    例如,运行以下管道将给出 您是一个显示所有字段的文档,但它们的值取决于显示子文档字段的内容;如果为真则返回实际值,否则返回空值:

    User.aggregate([
        {
            "$project": {
                "email": {
                    "$cond": ["$privacy.displayEmail", "$email", null]
                },
                "firstName": {
                    "$cond": ["$privacy.displayfirstName", "$firstName", null]
                },
                "lastName": {
                    "$cond": ["$privacy.displaylastName", "$lastName", null]
                },
                "username": {
                    "$cond": ["$privacy.displayUsername", "$username", null]
                }
            }
        }
    ]).exec(function(err, users){
        if (err) throw err;
        console.log(JSON.stringify(users, null, 4));
    })
    

    更新

    从 cmets 跟进您的其他问题,有条件地检查 addressphone 字段 AND 使用来自 GET 参数的 id 过滤集合,使用 @ 运行管道987654323@ 运算符充当查询,然后在 $project 中添加额外的信息字段条件显示:

    var userId = mongoose.Schema.Types.ObjectId(req.parameter.userId);
    
    User.aggregate([
        { "$match": { "_id": userId } },
        {
            "$project": {
                "email": {
                    "$cond": ["$privacy.displayEmail", "$email", null]
                },
                "firstName": {
                    "$cond": ["$privacy.displayfirstName", "$firstName", null]
                },
                "lastName": {
                    "$cond": ["$privacy.displaylastName", "$lastName", null]
                },
                "username": {
                    "$cond": ["$privacy.displayUsername", "$username", null]
                },
                "userAddress": {
                    "$cond": ["$privacy.displayAddress", "$extraInfo.userAddress", null]
                },
                "userPhone": {
                    "$cond": ["$privacy.displayPhone", "$extraInfo.userPhone", null]
                }
            }
        }
    ]).exec(function(err, users){
        if (err) throw err;
        console.log(JSON.stringify(users, null, 4));
    })
    

    【讨论】:

    • 我现在检查一下,我会返回结果,谢谢!
    • 谢谢您的回答,能否请您再检查一下问题,我添加了另一个需要有条件检查的对象。
    • 代码返回数据库中的所有用户,我如何将其限制为 GET 参数 id?
    • 避免以您的方式更改问题,在此非常不鼓励这样做。对此herehere 有元讨论。
    • 现在它返回一个空数组 [ ],我 console.log 记录 userId 就可以了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 2023-01-19
    • 2011-05-14
    相关资源
    最近更新 更多