简体   繁体   English

如何在 MongoDB C# 中只读取一个字段?

[英]How can I read just one field in MongoDB C#?

Im trying to read just one field from the MongoDB with a given ID, but its giving me all the object... How can I do it ?我试图从给定 ID 的 MongoDB 中只读取一个字段,但它给了我所有的对象......我该怎么做? What should I do?我应该怎么办? I tried with this repository but its returning me all the object我尝试使用此存储库,但它返回了我所有的对象

Here is my class:这是我的课:

public class Hall
{
  [BsonId]
  [BsonRepresentation(BsonType.ObjectId)]
  public string Id { get; set; }
  public string Name { get; set; }
  public List<string> Surveys { get; set; }
}

And here is my repository with the connection to Mongo:这是我与 Mongo 连接的存储库:

public Task<List<Hall>> ReadSurveysHall(string hallId)
{
  var filter = Builders<Hall>.Filter.(x => x.Id, hallId);
  return _mongoManager.Find(filter, MongoConstants.HallCollection);
}

I just want it to give me just the list of Survey's string.我只是希望它只给我调查字符串的列表。 Thank you in advance.先感谢您。

In order to return only the list of surveys, you can change your method like this:为了只返回调查列表,您可以像这样更改您的方法:

public async Task<List<string>> ReadSurveysHall(string hallId)
{
  var filter = Builders<Hall>.Filter.(x => x.Id, hallId);
  var halls = await _mongoManager.Find(filter, MongoConstants.HallCollection);
  var hall = halls.FirstOrDefault();
  if (hall == null)
    return new List<string>();
  return hall.Surveys;
}

You can further optimize this by using a projection so that MongoDB only delivers the data that you are interested in, eg:您可以通过使用投影进一步优化这一点,以便 MongoDB 仅提供您感兴趣的数据,例如:

var projection = Builders<Hall>.Projection.Exclude(x => x.Id).Include(x => x.Surveys);
var options = new FindOptions<Hall, Hall>() 
{
  Projection = projection,
};

How you use that projection depends on the capabilities of _mongoManager .如何使用该投影取决于_mongoManager的功能。 Take a look at the Find methods and check whether any of these accepts a projection or some kind of FindOptions as a parameter.查看Find方法并检查其中任何一个是否接受投影或某种FindOptions作为参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中将SOAP正文作为一个参数读取 - How can i read SOAP body as one parameter in C# C#:如何在一个文件中读取 'éóú' 和 öäü 等字符? - C#: How can I read characters like 'éóú' and öäü in one file? C# MongoDb - 如何更新嵌套文档中的一个字段? - C# MongoDb - How to update one field in nested documents? 如何在 C# 的 MongoDB 驱动程序中获取数组中一个字段的值 - How to get values of one field in array in MongoDB Driver for C# c#如何在richtextbox中只更新一行文本? - c# how can I update just one line text in richtextbox? 如何使用Line从C#数据集中获取一个数组? - How can I use Line to get just one array from a C# dataset? 查询 MongoDB C#:如何在 BsonDocument 中提取调用聚合方法产生的字段? - Query MongoDB C#: how can I extract in a BsonDocument the field that results from calling aggregate method? 如何使用c#将包含逗号的sql列作为一个字段读取 - how to read a sql column containing commas as one field with c# 我如何从C#中的mdr.Read()命令仅读取一条记录 - How Can i Read only one record from mdr.Read() command in c# 如何将此控制台mongodb查询转换为C#mongo驱动程序v2? - How can I convert this console mongodb query to a C# mongo driver v2 one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM