Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Its actually a pretty easy fix if you think about how the aggregation framework is described.</p> <p>Taken from the <a href="http://docs.mongodb.org/manual/core/aggregation/#pipelines" rel="nofollow">docs</a>:</p> <blockquote> <p>Conceptually, documents from a collection pass through an aggregation pipeline, which transforms these objects as they pass through. For those familiar with UNIX-like shells (e.g. bash,) the concept is analogous to the pipe (i.e. |) used to string text filters together.</p> </blockquote> <p>You may have read that before, but the reason to explain that again is that you can pass operations into that pipeline in just about any order - and more than once. Where as in MYSQL for example, <code>LIMIT</code> is always listed at the end of the query and applies to the result set after all other grouping functions.</p> <p>In MongoDB, the operations are run in the order you've added them to the pipeline. So order of operation matters.</p> <p>Looking at your your code above, it appears you're actually fetching everything - and depending on your IF statements, first ordering it, applying your offset and limit, then matching against that result set before projecting and grouping.</p> <p>So - long story short - it just looks like you need to reorder things:</p> <pre><code>pipelines = [] match = { 'discussion_id': { '$in': discussion_ids } } if start and end: match['created_at'] = { '$gte': start, '$lt': end } pipelines.append({ '$match': match }) if order: pipelines.append({ '$sort': { 'created_at': order } }) pipelines.append({ '$project': { 'poster_id': '$poster_id' } }) pipelines.append({ '$group': { '_id': '$poster_id', 'post_id': { '$first': '$_id' } } }) if skip: pipelines.append({ '$skip': skip }) if limit: pipelines.append({ '$limit': limit }) results = self.db.posts.aggregate(pipelines) </code></pre>
 

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