How to save entire MongoDB collection to json / bson file using C #?

I have a process that firstly generates a lot of data that is stored in a mongoDB collection, then the data is parsed and finally I want to save the entire collection to a file on disk and delete the collection. I know I can easily do this with MongoDump.exe, but I was wondering if there is a way to do this directly from C #? - I mean that you are not using console precession, but using some of the functionality found inside the MOngo C # driver.

And, if it can be done - how would I do the reverse operation in C #? - namely: loading a .bson file into a collection?

+3


source to share


2 answers


You can use C # BinaryFormatter to serialize the plot of an object to disk. Alternatively, you can deserialize back to a graphical object.

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

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



However, this is not a mongodb or C # function.

After serialization, you can use a driver to remove the collection. And after deserialization, you can use the driver to insert objects into the new collection.

Based on your rules, you might want to do some locks on this collection while you are doing the export process, before you release it.

+1


source


Here are two methods you can use to do 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 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();

      

Please note that this code was written using version 2.0 of the MongoDB C # driver, which you can get through Nuget . Also, the code for reading the file in the method LoadCollectionFromFile

was obtained from this answer .

+1


source







All Articles