node.js - Mongoose - 转到下一个元素

标签 node.js mongodb mongoose

我正在尝试在 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...

最佳答案

您可以研究一下 Mongoose 的流媒体功能:

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 ,这是 stream() 返回的,继承自 Node.js' Stream , 所以你可以使用 pause 做一些有趣的事情和 resume如果需要的话。

[编辑]

现在我对您的问题有了更多了解,我想说 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') 时,返回的 User 上的 lastQuote 属性将是实际引用的 Quote 对象通过存储在 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);
    }
  });
};

这是找到下一个引号或环绕到第一个引号的部分。

查看代码,如果您有任何问题,请告诉我。

关于node.js - Mongoose - 转到下一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12096516/

相关文章:

javascript - Node JS 数组、Foreach、Mongoose、同步

javascript - Mongoose ODM : Can't save encoded password because of async

node.js - 如何通过 $lookup 对 'joined' 集合执行 $text 搜索?

mysql - 为什么response.write()只打印node.js中的第一行?

node.js - 检查redis变量是否在nodejs中从外部更改

json - 如何使用 twit 通过媒体 POST 请求执行更新?

MongoDB 使用错误的索引

node.js - Nodejs HTTP/1.1 管道支持

java - 使用 Spring-data 在 java.util.List 中插入 $currentDate

security - 无法使用 MongoDB super 用户从终端连接到其他数据库