1

I can do a find in mongodb like:

db.user.find({ "show": true}, { firstname: 1, lastname: 1, email:1 })

Now I'd like to be a little more specific to let people hide their email address. So what I'd like is to query if "show":true then get firstname:1, lastname:1 AND (email:1 iff show_email:true)

worst case I can take all the info and filter it out on the back end with a map function but I wondered if there was a nice query-able way??

2 Answers 2

1

The truthiness of the projection object's fields are all that matter, so you can just do it as:

db.user.find({ "show": true }, { firstname: 1, lastname: 1, email: show_email })
Sign up to request clarification or add additional context in comments.

Comments

0

Since you didnt specify any driver specification, I am betting on this, the MongoShell

if(show_email){
   db.user.find({ "show": true}, { firstname: 1, lastname: 1, email:1 })
}
else{
   db.user.find({ "show": true}, { firstname: 1, lastname: 1})
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.