如何使用C#将整个MongoDB集合保存到json / bson文件?

时间:2015-08-06 23:05:40

标签: c# json mongodb bson

我有进程,首先,生成大量数据,保存到mongoDB集合中,然后分析数据,最后 - 我想将整个集合保存到磁盘上的文件,并擦除集合。 我知道我可以用MongoDump.exe轻松完成,但我想知道有没有办法直接从c#中做到这一点? - 我的意思是不使用控制台进程 - 但使用MOngo C#驱动程序中的一些功能。

并且,如果可以这样做 - 我将如何在c#中执行反向操作? - 即:将.bson文件加载到集合中?

2 个答案:

答案 0 :(得分:1)

您可以使用C#BinaryFormatter将对象图序列化为磁盘。您也可以反序列化回对象图。

序列化: https://msdn.microsoft.com/en-us/library/c5sbs8z9%28v=VS.110%29.aspx

反序列化: https://msdn.microsoft.com/en-us/library/b85344hz%28v=vs.110%29.aspx

然而,这不是mongodb或C#驱动程序功能。

序列化后,您可以使用驱动程序删除集合。反序列化后,您可以使用驱动程序将对象插入到新集合中。

根据您的规则,您可能希望在删除之前对该集合执行某些锁定。

答案 1 :(得分:1)

Here's two methods that you can use to accomplish this:

public static async Task WriteCollectionToFile(IMongoDatabase database, string collectionName, string fileName)
{
    var collection = database.GetCollection<RawBsonDocument>(collectionName);

    // Make sure the file is empty before we start writing to it
    File.WriteAllText(fileName, string.Empty);

    using (var cursor = await collection.FindAsync(new BsonDocument()))
    {
        while (await cursor.MoveNextAsync())
        {
            var batch = cursor.Current;
            foreach (var document in batch)
            {
                File.AppendAllLines(fileName, new[] { document.ToString() });
            }
        }
    }
}

public static async Task LoadCollectionFromFile(IMongoDatabase database, string collectionName, string fileName)
{
    using (FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    using (BufferedStream bs = new BufferedStream(fs))
    using (StreamReader sr = new StreamReader(bs))
    {
        var collection = database.GetCollection<BsonDocument>(collectionName);

        string line;
        while ((line = sr.ReadLine()) != null)
        {
            await collection.InsertOneAsync(BsonDocument.Parse(line));
        }
    }
}

And here's an example of how you would use them:

// Obviously you'll need to change all these values to your environment
var connectionString = "mongodb://localhost:27017";
var database = new MongoClient(connectionString).GetDatabase("database");
var fileName = @"C:\mongo_output.txt";
var collectionName = "collection name";

// This will save all of the documents in the file you specified
WriteCollectionToFile(database, collectionName, fileName).Wait();

// This will drop all of the documents in the collection
Task.Factory.StartNew(() => database.GetCollection(collectionName).DeleteManyAsync(new BsonDocument())).Wait();

// This will restore all the documents from the file you specified
LoadCollectionFromFile(database, collectionName, fileName).Wait();

Note that this code was written using version 2.0 of the MongoDB C# driver, which you can obtain via Nuget. Also, the file reading code in the LoadCollectionFromFile method was obtained from this answer.