Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After thinking too much, I figured out a solution using map-reduce since use aggregation framework seems to be not possible.</p> <p>Here are the reduce function, that simple group documents by <code>user_id</code>.</p> <pre><code>var mapf = function () { emit(this.user_id, { _id: this._id, creation_time: this.creation_time }) } </code></pre> <p>On reduce function, I check if there is at least four entries. If true, <code>values</code> array is sorted by <code>creation_time</code> and the first 4 documents are skipped.</p> <pre><code>var redf = function (key, values) { var result = {}; if (values.length &gt; 4) { values.sort(function (a, b) { return a.creation_time &gt; b.creation_time; }); // unfortunately, mongodb doesn't support array as result of reduce function result['oids'] = values.slice(3); } return result; } </code></pre> <p>Now it's time to run map-reduce command. The results will be inserted in <code>plus_four_users</code> collection.</p> <pre><code>db.newsdb.mapReduce(mapf, redf, { out : "plus_four_users" }) </code></pre> <p>This will result in something like that:</p> <pre><code>&gt; db.newsdb.find({}, { user_id : 1, creation_time : 1 }) { "_id" : ObjectId("51d612423dab6225ca6e6d36"), "creation_time" : 100, "user_id" : 10 } { "_id" : ObjectId("51d612503dab6225ca6e6d37"), "creation_time" : 200, "user_id" : 10 } { "_id" : ObjectId("51d612553dab6225ca6e6d38"), "creation_time" : 300, "user_id" : 10 } { "_id" : ObjectId("51d612593dab6225ca6e6d39"), "creation_time" : 400, "user_id" : 10 } { "_id" : ObjectId("51d6125d3dab6225ca6e6d3a"), "creation_time" : 500, "user_id" : 10 } { "_id" : ObjectId("51d6126f55ebf2ff5a13d1c9"), "creation_time" : 600, "user_id" : 10 } { "_id" : ObjectId("51d6127455ebf2ff5a13d1ca"), "creation_time" : 300, "user_id" : 11 } { "_id" : ObjectId("51d6127955ebf2ff5a13d1cb"), "creation_time" : 400, "user_id" : 11 } { "_id" : ObjectId("51d6127c55ebf2ff5a13d1cc"), "creation_time" : 500, "user_id" : 11 } { "_id" : ObjectId("51d6127f55ebf2ff5a13d1cd"), "creation_time" : 600, "user_id" : 11 } { "_id" : ObjectId("51d6128555ebf2ff5a13d1ce"), "creation_time" : 700, "user_id" : 11 } &gt; db.plus_four_users.find().pretty() { "_id": 10, "value": { "oids": [ { "_id": ObjectId("51d6125d3dab6225ca6e6d3a"), "creation_time": 500 }, { "_id": ObjectId("51d6126f55ebf2ff5a13d1c9"), "creation_time": 600 } ] } } { "_id": 11, "value": { "oids": [ { "_id": ObjectId("51d6128555ebf2ff5a13d1ce"), "creation_time": 700 } ] } } </code></pre> <p>Hope that can help you!</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.
    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