4

I am using MongoDB in our project and I'm currently learning how things work

I have created a collection with 5 million records. When i fire the query db.ProductDetails.find() on the console it takes too much time to display all the data.

Also when i use the following code in C#

var Products = db.GetCollection("ProductDetails").FindAll().Documents.ToList();

the system throws OutOfMemoryException after some time..

Is there any other faster or more optimized way to achieve this ?

Community
  • 1
  • 1
Kushal Shah
  • 55
  • 2
  • 6

2 Answers2

4

Never try to fetch all entries at the same time. Use filters or get a few rows at a time.

Read this question: MongoDB - paging

Community
  • 1
  • 1
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • 1
    Actually i need entire collection at once initially – Kushal Shah Jul 01 '11 at 07:30
  • which provider do you use to fetch all objects? and how large are each object? – jgauffin Jul 01 '11 at 07:32
  • I use the following Code Mongo firsttest = new Mongo(); firsttest.Connect(); var db = firsttest.GetDatabase("myDB"); var Products=db.GetCollection("ProductDetails").FindAll().Documents.ToList(); – Kushal Shah Jul 01 '11 at 07:36
  • which provider (like NoRM) do you use to fetch all objects? and how large are each object (in bytes) ? And ***why*** do you need to fetch all objects? – jgauffin Jul 01 '11 at 07:40
  • I use MongoDBDriver-Release-0.90.0-Beta-1 which is suggested at mongodb.org. The Collection contain following Column Person,Product,Contient,Country,State,City,Qty,Amount – Kushal Shah Jul 01 '11 at 07:53
  • var Products=db.GetCollection("ProductDetails").FindAll().Documents This line is executed within no time. But how should i convert the Products variable to any list or array type ?Converting takes time here – Kushal Shah Jul 01 '11 at 07:55
  • 1
    Why do you need to fetch all objects? Why can't you fetch a few items at a time? Deserialization will take a lot of memory and so will keeping 5M entities in memory. – jgauffin Jul 01 '11 at 07:55
0

Try to get the subset which is needed. If you try to fetch all objects, then it is for sure you will need enough RAM as the size of your database collection !! Try to fetch the objects which will be used in the application.

rAJesh
  • 1
  • 2
    Welcome to Stackoverflow. Don't forget to visit [this](http://stackoverflow.com/questions/how-to-answer). – menjaraz Jan 02 '12 at 06:31