我试图在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'
Run Code Online (Sandbox Code Playgroud)
到目前为止最好的想法是在我的模式中添加一个链表,这样我就可以在下一次对特定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 …
Run Code Online (Sandbox Code Playgroud)