1

I'm creating a tool for our data, reading data with Entity Framework from our SQL Server database and inserting them, row by row, in our MongoDB database.

In my code, I start in this way:

_client = new MongoClient();
_database = _client.GetDatabase("isovtest");

Then, with a loop on my SQL Server table

 foreach (var erog in db.transactions)

I create a Bson document for each record.

document = new BsonDocument{....}

and finally I insert it:

_database.GetCollection<BsonDocument>("transactions").InsertOne(document);

My problem is the source table is big, with some millions of records. After about 400K, it crashes every time because it goes out of memory (about 1,6 GB occupied in my RAM: at this size it crashes, also if there is still free memory).

I think the problem is the instruction

_database.GetCollection<BsonDocument>("transactions").InsertOne(document);

since, with GetCollection, it retrieve all the data, getting a grower collection every loop.

I tried to add, at the end of my loop:

document = null;
GC.Collect();

but nothing changed.

So, can you tell me if exists another way to insert a document in my MongoDB collection without using the GetCollection method? If not, how can avoid the memory problem?

rnofenko
  • 9,198
  • 2
  • 46
  • 56
Piero Alberto
  • 3,823
  • 6
  • 56
  • 108
  • GetCollection doesn't consume memory because it doesn't load any objects from DB. I am sure you have memory leaks when you load data from SQL server. Can you show your code? – rnofenko Nov 30 '16 at 13:51
  • Honestly, I made it, but I have a similar problem when I try to get data from my mondoDB instance. The mongod.exe process grows and reaches about 3,5 GB inmy ram... I do it in this way: – Piero Alberto Dec 01 '16 at 14:42
  • var filter = Builders.Filter.Eq("storeid", "01"); var collection = _database.GetCollection("transactions"); var result = await collection.Find(filter).ToListAsync(); – Piero Alberto Dec 01 '16 at 14:43
  • I don't get any result (I can ensure you it has something to return) – Piero Alberto Dec 01 '16 at 14:43
  • When you have big enough result you should get data through Cursor instead of ToListAsync. – rnofenko Dec 01 '16 at 21:06
  • @mofenko can you give/link me an example? I am a noob here – Piero Alberto Dec 05 '16 at 11:11

0 Answers0