Alucard
Alucard

Reputation: 1902

Fetching mongodb one by one

I have a collection named Questions.

I want to fetch first question, then when user clicks a button, give him the next..

So i'm fetching the first item like this:

Template.home.user_questions = function () {
 return Questions.find({}, {sort: {answer1:{'$ne': ''}}, limit: 1});
}

And I'm getting the user's click like this:

'click input' : function () {
  Questions.update(this._id, {$inc: {value1: 1}})
  // now show him next item
}

But I can't seem to figure out how to show the next item since I hasNext() and next() methods are not supported by meteor

Upvotes: 0

Views: 53

Answers (1)

Tarang
Tarang

Reputation: 75945

You could use skip.

Template.home.user_questions = function () {
    var skip = Session.get("skip") || 0;
    return Questions.find({}, {sort: {answer1:{'$ne': ''}}, limit: 1, skip: skip});
}

Then when you want to move to the next question, augment the session value of skip by 1. e.g

Session.set("skip", (Session.get("skip") || 0) ++);

This should reactively move to the next question, up to the last.

Upvotes: 1

Related Questions