nodejs のコレクションで異なる ID を反復処理しようとしています。次のコードのように機能するもの:
//Callbacks removed for readability
var thisPost = mongoose.model('Post').findOne({tags: 'Adventure'});
console.log(thisPost.title); // 'Post #1 - Adventure Part 1'
var nextPost = thisPost.next({tags: 'Adventure');
console.log(nextPost.title); // 'Post 354 - Adventure Part 2'
これまでの最善のアイデアは、スキーマにリンクリストを追加して、特定の ID への次の参照で find() を呼び出すことができるようにすることですが、この Mongoose 参照 (thisPost) を使用できるようにするための「トリッキー」ではないものを望んでいました。私のfind()が開始できるカーソルとして。
ありがとう
編集:反復は、複数のページクエリで機能することを意図しています。より良い例:
//Callbacks removed for readability
//User 'JohnDoe' visits the website for the first time
var thisQuote = mongoose.model('Quote').findOne().skip(Math.rand());
res.send(thisQuote); // On page output, JohnDoe will see the quote 42
//Saving the current quote cursor to user's metadatas
mongoose.model('User').update({user: 'JohnDoe'}, {$set: {lastQuote: thisQuote }});
//User 'JohnDoe' comes back to the website
var user = mongoose.model('User').findOne({user: 'JohnDoe});
var thisQuote = user.lastQuote.next();
res.send(thisQuote); // On page output, JohnDoe will see the quote 43
//Saving the current quote cursor to user's metadatas
mongoose.model('User').update({user: 'JohnDoe'}, {$set: {lastQuote: thisQuote }});
//And so on...