Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB C#迭代作为枚举器_C#_.net_Mongodb .net Driver - Fatal编程技术网

MongoDB C#迭代作为枚举器

MongoDB C#迭代作为枚举器,c#,.net,mongodb-.net-driver,C#,.net,Mongodb .net Driver,我收集了大量MongoDB(>10m)的项目 代码是用C#/.NET编写的 有时,我需要遍历所有文档来修剪数据,进行一些其他维护,等等。 此外,在某些情况下,我需要能够遍历所有文档,但只获取每个文档的Id 我希望文档以IEnumerable的形式呈现,供用于处理列表等的代码使用 我做了以下工作: private static IAsyncCursor<MongoAlbum> GetCursor(Parameters...) { var F = Make

我收集了大量MongoDB(>10m)的项目

代码是用C#/.NET编写的

有时,我需要遍历所有文档来修剪数据,进行一些其他维护,等等。 此外,在某些情况下,我需要能够遍历所有文档,但只获取每个文档的Id

我希望文档以IEnumerable的形式呈现,供用于处理列表等的代码使用

我做了以下工作:

    private static IAsyncCursor<MongoAlbum> GetCursor(Parameters...)
    {
        var F = MakeFilter(Parameters...);
        var Cursor = Mongo.Driver.FindAsync(F).Result;
        return Cursor;
    }

    internal static IEnumerable<string> IterateThroughAlbumIds(Parameters...)
    {
        using (var Cursor = GetCursor(Parameters...))
        {
            while (Cursor.MoveNextAsync().Result)
            {
                var Batch = Cursor.Current;
                foreach (var Document in Batch) yield return Document._id;
            }
        }
    }

    internal static IEnumerable<MongoAlbum> IterateThroughAlbums(Parameters...)
    {
        using (var Cursor = GetCursor(Parameters...))
        {
            while (Cursor.MoveNextAsync().Result)
            {
                var Batch = Cursor.Current;
                foreach (var Document in Batch) yield return Document;
            }
        }
    }
专用静态IAsyncursor GetCursor(参数…)
{
var F=MakeFilter(参数…);
var Cursor=Mongo.Driver.FindAsync(F).Result;
返回光标;
}
内部静态IEnumerable迭代器ID(参数…)
{
使用(var Cursor=GetCursor(参数…)
{
while(Cursor.MoveNextAsync().Result)
{
var Batch=Cursor.Current;
foreach(批处理var单据)收益返还单。\u id;
}
}
}
内部静态IEnumerable迭代相册(参数…)
{
使用(var Cursor=GetCursor(参数…)
{
while(Cursor.MoveNextAsync().Result)
{
var Batch=Cursor.Current;
foreach(批处理var单据)收益返还单;
}
}
}
现在我想知道两件事:

  • 有没有更好的方法使异步枚举看起来像.NET IEnumerable
  • 在枚举过程中,我如何告诉驱动程序只返回文档的id

您可以使用ToEnumerable扩展方法来实现这一点,并且只需使用投影即可获得所需的ID

下面的代码应该可以工作

public class MongoAlbum
{
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
    public string Property { get; set; }
}
IMongoClient client;
IMongoDatabase database;
IMongoCollection<MongoAlbum> collection;
client = new MongoClient("connection string");
database = client.GetDatabase("DB name");
collection = database.GetCollection<MongoAlbum>("collection name");
IEnumerable<string> albums = collection.Find(x => x.Id == "1")
                                       .Project(x => x.Id)
                                       .ToEnumerable<string>();
公共类MongoAlbum
{
[BsonRepresentation(BsonType.ObjectId)]
公共字符串Id{get;set;}
公共字符串属性{get;set;}
}
IMongoClient客户端;
IMONGO数据库;
IMongoCollection;
客户端=新的MongoClient(“连接字符串”);
database=client.GetDatabase(“DB name”);
collection=database.GetCollection(“集合名称”);
IEnumerable albums=collection.Find(x=>x.Id==“1”)
.Project(x=>x.Id)
.ToEnumerable();

在这种情况下,它将是一个字符串列表,因为您将只获得ID结果,并且在我的MongoAlbum POCO中,我使用了一个字符串。

您使用了可数扩展方法,并且为了仅获得ID,您需要使用投影

下面的代码应该可以工作

public class MongoAlbum
{
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
    public string Property { get; set; }
}
IMongoClient client;
IMongoDatabase database;
IMongoCollection<MongoAlbum> collection;
client = new MongoClient("connection string");
database = client.GetDatabase("DB name");
collection = database.GetCollection<MongoAlbum>("collection name");
IEnumerable<string> albums = collection.Find(x => x.Id == "1")
                                       .Project(x => x.Id)
                                       .ToEnumerable<string>();
公共类MongoAlbum
{
[BsonRepresentation(BsonType.ObjectId)]
公共字符串Id{get;set;}
公共字符串属性{get;set;}
}
IMongoClient客户端;
IMONGO数据库;
IMongoCollection;
客户端=新的MongoClient(“连接字符串”);
database=client.GetDatabase(“DB name”);
collection=database.GetCollection(“集合名称”);
IEnumerable albums=collection.Find(x=>x.Id==“1”)
.Project(x=>x.Id)
.ToEnumerable();

在本例中,它将是一个字符串列表,因为您将只获得Ids结果,并且在我的MongoAlbum POCO中,我使用了一个字符串。

异步可枚举在.NET中仍然是一个有问题的概念。当然有Rx和Ix(实体框架异步构建在后者的概念上),但两者都没有统一的支持。更麻烦的是异步->同步逻辑转换(这就是您在这里看到的)。如果您必须有一个
IEnumerable
并同步调用它,并且您希望以最小的编码工作量来完成此操作,那么您所做的就可以了(尽管我使用
GetAwaiter().GetResult()
进行阻塞,而不是
Result
进行更干净的异常传播).Async enumerable在.NET中仍然是一个有问题的概念。当然有Rx和Ix(实体框架异步构建在后者的概念上),但两者都没有统一的支持。更麻烦的是异步->同步逻辑转换(这就是您在这里看到的)。如果您必须有一个
IEnumerable
并同步调用它,并且您希望以最小的编码工作量来完成此操作,那么您所做的就可以了(尽管我将使用
GetAwaiter().GetResult()
进行阻塞,而不是使用
Result
进行更干净的异常传播)。