Get sizes of documents of all collections in a database in MongoDB

So, this time just a useful snippet that can be executed by mongo myFile.js  to print som statitsitcs about documents in your MongoDB. db.collection.stats() has more useful information.

 

GD Star Rating
loading...

Using map-reduce with MongoDB

Besides its other features MongoDB also support aggreagtions within the database. One way is to use the built-in aggreagtion framework and the other map-reduce.
From my point of view the aggregation frameworks works good for simple aggregations but for complicated stuff like summing up and also average calcluations map-reduce is the better approach.

The only thing that went wrong in the beginning is that map and reduce have to return the same data because reducing is only a step for summing up all the stuff. Average calculation has to be done in a final step, called finalize.

The output is written to a collection.

See the examples here.

As a performance indicator I can say that it aggregates 4 mio documents (summing up some internal structures and taking averages on main categories and overall) in about 5 minutes and was 4 hours with pure Java implementation before.

GD Star Rating
loading...