【问题标题】:Mongoose - Go to next elementMongoose - 转到下一个元素
【发布时间】:2012-08-25 02:08:19
【问题描述】:

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

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您可能会研究 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,因此您可以根据需要使用pauseresume 做一些有趣的事情。

    [编辑]

    现在我对您的问题有了更多了解,我想说 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);
        }
      });
    };
    

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

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

    【讨论】:

    • 最后没有使用这个版本但是是的,从文档来看,流是做我想做的最好的方式。虽然我不会使用它的原因有很多,这取决于我的应用程序当前的代码架构:)
    • 哇。我昨天在我的数据库中使用链表实现了它,但是你的答案很简单,我为自己没有想到它而深感羞愧。感谢您的思考和一百万谢谢您的回答:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    相关资源
    最近更新 更多