Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

396
Views
How to return specific object values related to privacy values?

I have this mongoose model:

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

Let's assume that the user sets the value of displayEmail abd displayLastName to false.

Then, I have a simple GET request that returns a json object with all the users details. How can I query mongo to return only the fields that have true in the privacy object? If displayfirstName is true, then the firstName value should be returned.

Update

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

How i can conditionally check the address and phone field?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The only way I can think of is through an aggregation pipeline that has a $project operator with a $cond expression that evaluates the condition and returns the value of one of the other two expressions depending on the logic evaluated.

Take for instance, running the following pipeline will give you a document that displays all the fields but their values depend on what the display subdocument fields have; if it's true then the actual value is returned otherwise a null value is returned:

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

UPDATE

Following up your other question from the comments, to conditionally check for the address and phone fields AND filter the collection using the id from a GET parameter, run the pipeline with a $match operator which acts as a query and then add the extra info fields conditional display in your $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));
})
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!