Meteor, Mongo request to find every nth document

I use timestamps in my collection, so every document has a timestamp, the user wants to get documents from " ts1 " (timestamp 1) to " ts2 " (timestamp 2), however there are too many documents in this interval, so I won’t return only every nth, for example, if there are 100,000 documents, I need to display 1000 documents, so 100000/1000 = 100. every 100th document.

Is this possible, and how can I achieve this.

PS. I need to request this inside the Meteor publishing method.

Here is what I got so far:

Meteor.publish('documents-chunk', function (from, to) {
    //get find documents count and get nth
    var count = Documents.find({time: {$gte: from, $lte: to}}).count();
    if (count > 2000) {
        var nth = Math.round(count / 1000);
        return Documents.find(/*query every nth*/);
    }
    return Documents.find({time: {$gte: from, $lte: to}});
});

DECISION:

I solved this problem using an answer from Matt K.

, : "id":

**

1.

**

Document.find({}, {sort: {time: 1}}).forEach(function (c, i) {
    Document.update(c, {$set: {id: i + 1}});
    console.log(i + 1);
});

1,5 , ( , index {time: 1} , )

**

2.

**

Meteor.publish('documents-chunk', function (from, to) {
    var nth = Math.round(Documents.find({time: {$gte: from, $lte: to}}, {sort: {time: 1}}).count() / 1000);
    return Documents.find({time: {$gte: from, $lte: to, $mod: [nth, 0]}}, {sort: {time: 1}});
});

, , ;

http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/, . , , https://jira.mongodb.org/browse/SERVER-2397, , , , .

+4
2

, , . :

100 000, 1000-. , , . , , .

, _id ( ) . var qCount = cursor.count(). _id % qCount === 0.

sample 1000- , : {$exists: {sample: true}}

-. 1000- ? " ", , , , . ( , - ...)

+1

, mongoDB _id , _id N . N .

Meteor.publish('documents-chunk', function (from, to) {
  return Documents.find({time: {$gte: from, $lte: to}},{sort: {_id: 1}, {limit: 1000}});
});

, .

+1

All Articles