0

I have a User.cs model as follows:

uid: string
name: string
email: string
password: string
friends: List<string>

I get all users from a mongodb collection using the following code:

List<User> users = null;
var query = await _userCollection.FindAsync<User>(user => true);
users = await query.ToListAsync();

The elements in the users list have the same fields as the User model.

Now I want the returned elements or documents to have every field except password. I searched online and found that we can use SetFields but I am not able to found any code or links to follow. If possible, I would like to keep the method async.

Thanks for helping!

echoaman
  • 71
  • 4
  • 15
  • 1
    FYI: You should not be storing plain-text passwords. Please make sure you understand proper security if this is something with real users. – Mani Gandham Apr 30 '21 at 20:20

1 Answers1

1

if you need just to exclude the password field from the result, you need to use Projection: https://digitteck.com/mongo-csharp/projections_in_mongo_csharp/. Also the second answer here: Mongodb -- include or exclude certain elements with c# driver (the first one is about legacy driver)

dododo
  • 3,872
  • 1
  • 14
  • 37