Note that there are some explanatory texts on larger screens.

plurals
  1. PO'AVG' and 'SUM' functionality in MongoDB, any tips?
    primarykey
    data
    text
    <p>I'm a relative newbie to MongoDB, but from what I've read there are various methods to going about finding averages and sums of values in a MongoDB database, with various benefits and drawbacks for each.</p> <p>I'm primarily asking for a method of finding the sum of a selection of values, and the average of a selection of values, in an as efficient (fast) method possible.</p> <p>The documents in the collection being queried resemble this structure (with a lot of other fields):</p> <pre><code>{ "_id": ObjectId('4e650107580fd649e5000005'), "date_added": ISODate("2011-09-05T00:00:00Z"), "value": 1500 } </code></pre> <p>Precalculating things like sums is, in my application, not always possible, because the selection of values to be summed can change (based on date ranges - e.g. between a start date and an end date, what is the average). This is a similar problem with precalculating averages.</p> <p>From what I've read, MapReduce is definitely not ideal for real-time (i.e. on demand) lookup, so that seems to be out of the question too.</p> <p>At the moment I'm querying the collection in this way: (note: this is using <code>pymongo</code>)</p> <pre><code>response = request.db['somecollection'].find( { 'date_added': { '$gte': date_start, '$lte': date_end } }, { 'value':1 } ).limit(500) </code></pre> <p>Then doing the calculation in Python using a <code>for</code> loop over the response. The limit of 500 results is arbitrary, to keep it from become too slow. I'm only retrieving the value, and none of the other fields.</p> <p>Is this the most efficient method of doing this calculcation, or are there other methods to accomplish what I need?</p> <p><strong>Caveats:</strong></p> <ul> <li>I can't use the <code>group</code> function because I will probably be using sharding in the future</li> <li>I can't use MapReduce because it's a function which will be used on-the-fly by users</li> <li>I can't precalculate a lot of my sums/averages because the selection of values to sum/average is almost always different</li> <li>I have looked around stackoverflow and the web to try and find recommendation on how to do this kind of thing, and it's fairly open-ended</li> </ul> <p><strong>EDIT:</strong></p> <p>I should point out that the number of documents returned from the query I posted above could be anything from 1 document to hundreds, but will probably have a maximum number of returned documents of about 150 (average of about 60 or 70)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload