2012-08-23 10 views
5

nodejsのコレクションで異なるIDを繰り返し処理しようとしています。次のコードのように働く何か:Mongoose - 次の要素に移動

//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に私の次の参照の上に()私のスキーマにLinkedListのを追加することですが、私は何かを期待していましたこれは、私のfind()が始まるカーソルとして、このMongooseリファレンス(thisPost)を使用することを可能にします。

ありがとうございました

編集:この繰り返しは、複数のページのクエリを処理するためのものです。より良い例:

//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... 

答えて

11
あなたはマングースのストリーミング機能に見えるかもしれません

var stream = mongoose.model('Post').find({tags: 'Adventure'}).stream(); 

// Each `data` event has a Post document attached 
stream.on('data', function (post) { 
    console.log(post.title); 
}); 

QueryStream、あなた場合はpauseresumeを使用して、いくつかの興味深いことを行うことができますのでstream()戻ると、Node.js' Streamから継承するものです必要があります。

[編集]

今、私はもう少しあなたの質問を理解していることを、私はQueryStreamはあなたが使用したいものではありませんおそらくであると言うでしょう。私は今日これで少し作業をして、https://gist.github.com/3453567で解決策を得ました。 GIST(git://gist.github.com/3453567.git)を複製するだけでnpm installを実行し、次にnode index.jsを実行すると、http://localhost:3000にアクセスできます。ページを更新すると、「次の」見積もりが得られます。最後に達すると、それは折り返されます。

これが原因カップルの事で動作します:

まず、我々は彼らのデータにユーザーの「最後に表示した」見積書にreferenceを保存:今すぐ

var UserSchema = new mongoose.Schema({ 
    user: String, 
    lastQuote: { type: mongoose.Schema.Types.ObjectId, ref: 'Quote' } 
}); 

我々はUser.findOne().populate('lastQuote')を行い、lastQuote属性戻り値を返すUserは、MongoDB(ObjectId)に格納されているフィールドの値によって参照される実際のQuoteオブジェクトになります。

私たちは、次の理由コードで、この引用オブジェクトにnext()を呼び出すことができます。

QuoteSchema.methods.next = function(cb) { 
    var model = this.model("Quote"); 
    model.findOne().where('_id').gt(this._id).exec(function(err, quote) { 
    if (err) throw err; 

    if (quote) { 
     cb(null, quote); 
    } else { 
     // If quote is null, we've wrapped around. 
     model.findOne(cb); 
    } 
    }); 
}; 

これは、次の引用を見つけたり、他の最初の引用符にラップアラウンド部分です。

コードを見て、ご質問がある場合はお知らせください。

+0

最後にこのバージョンを使用していませんでしたが、ドキュメントから判断すると、ストリームは私がしたいことを行う最善の方法です。私のアプリケーションの現在のコードアーキテクチャに依存する多くの理由でそれを使用しません:) – red

+0

あなたのアップデートに基づいて私の答えを変更しました。 –

+0

うわー。 私は昨日、データベースのリンクリストを使って実装しましたが、あなたの答えはとてもシンプルです。私はそれを自分で考えないと深く恥じています。あなたの考えのための誇りと百万人は答えに感謝します:) – red

関連する問題