C# Mongodb - 只获取文档的指定字段
C# Mongodb - Get only the specified fields of documents
我有一个 User.cs
模型如下:
uid: string
name: string
email: string
password: string
friends: List<string>
我使用以下代码从 mongodb 集合中获取所有用户:
List<User> users = null;
var query = await _userCollection.FindAsync<User>(user => true);
users = await query.ToListAsync();
users
列表中的元素与 User
模型具有相同的字段。
现在我希望返回的元素或文档具有除 password
之外的每个字段。我在网上搜索并发现我们可以使用 SetFields
但我找不到任何代码或链接。如果可能的话,我想保留方法 async
.
感谢您的帮助!
如果您只需要从结果中排除密码字段,则需要使用投影:https://digitteck.com/mongo-csharp/projections_in_mongo_csharp/. Also the second answer here: Mongodb -- include or exclude certain elements with c# driver(第一个是关于遗留驱动程序的)
我有一个 User.cs
模型如下:
uid: string
name: string
email: string
password: string
friends: List<string>
我使用以下代码从 mongodb 集合中获取所有用户:
List<User> users = null;
var query = await _userCollection.FindAsync<User>(user => true);
users = await query.ToListAsync();
users
列表中的元素与 User
模型具有相同的字段。
现在我希望返回的元素或文档具有除 password
之外的每个字段。我在网上搜索并发现我们可以使用 SetFields
但我找不到任何代码或链接。如果可能的话,我想保留方法 async
.
感谢您的帮助!
如果您只需要从结果中排除密码字段,则需要使用投影:https://digitteck.com/mongo-csharp/projections_in_mongo_csharp/. Also the second answer here: Mongodb -- include or exclude certain elements with c# driver(第一个是关于遗留驱动程序的)