Mongo的Cursor.nextObject有时错误地返回Null吗? [英] Mongo's Cursor.nextObject Sometimes Erroneously Returns Null?

查看:76
本文介绍了Mongo的Cursor.nextObject有时错误地返回Null吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在组合async.queueCursor.nextObject以遍历游标,并对返回的文档进行一些异步工作.

I'm combining async.queue and Cursor.nextObject to iterate over a cursor and do some asynchronous work against the returned documents.

已经有一个很棒的小程序包, https://www.npmjs. org/package/mongo-cursor-processing ,但不幸的是它没有公开我需要的底层队列.

There's a great little package that already does this, https://www.npmjs.org/package/mongo-cursor-processing, but it unfortunately doesn't expose the underlying queue, which I need.

因此,我尝试自己实施,但遇到了麻烦.有时候,当实际上有更多文档时,Cursor.nextObject返回null.

So, I tried implementing it myself, but have hit a snag. Sometimes, Cursor.nextObject returns null when in fact there are more documents.

这是我附加到队列的一些代码段,以说明:

Here's a little code snippet that I've attached to the queue to illustrate:

if (this.cursor && this.length() < this.concurrency) {
    this.cursor.nextObject(function(err, item) {
        console.log(this.name + ': ' + (item ? item._id : '<null>') + ' ' + (err ? err : '<null>'));
        if (item) {
            this.push(item);
        } else {
            // delete this.cursor;
        }
    }.bind(this));
}

控制台日志显示:

... Maybe 100 lines ...
prop-queue: 511abbd59c0d972a3e000119 <none>
prop-queue: 511abbd59c0d972a3e00011d <none>
prop-queue: 511abbd59c0d972a3e000120 <none>
prop-queue: 511abbd59c0d972a3e000122 <none>
prop-queue: <none> <none>
prop-queue: 511abbd59c0d972a3e000125 <none>
prop-queue: 511abbd59c0d972a3e000127 <none>
prop-queue: 511abbd59c0d972a3e000129 <none>
prop-queue: 511abbd59c0d972a3e00012c <none>
... 1000's more lines before the next null ...

有时,在下一次调用成功之前,<none> <none>行会重复两次.

Sometimes, the <none> <none> line is repeated twice before the next call succeeds.

真正有趣的部分是当我在Mongo shell中执行查询时,在将511abbd59c0d972a3e000122511abbd59c0d972a3e000125打印到控制台之间会有一个暂停.暂停持续约0.75秒,并且恰好在光标中击中空文档的位置.我已经遍历了查询中的成千上万个文档,这是我经历的唯一的停顿.此外,检查null两侧的两个文档都没有任何特殊之处.

The really interesting part is when I execute the query in the Mongo shell, there's a pause in between when 511abbd59c0d972a3e000122 and 511abbd59c0d972a3e000125 are printed to console. The pause lasts about 0.75s, and is right where the null document is hit in the cursor. I've iterated over thousands of documents in the query, and it's the only pause I experienced. Furthermore, examining the two documents on either side of the null shows no peculiarities.

任何想法都可能导致这两种可能的相关现象吗?

Any ideas what could cause the two likely-related phenomena?

推荐答案

我仍然不确定是什么原因导致了暂停,但似乎是罪魁祸首.

I'm still not sure what's causing the pause, but it seems that is the culprit.

在暂停期间,Cursor.nextObject在第一个返回之前被调用多次.其中一些调用返回null.解决方案是确保不会同时调用Cursor.nextObject.

During the pause, Cursor.nextObject is getting called several times before the first returns. Some of these calls are returning null. The solution is to make sure Cursor.nextObject is never called concurrently.

if (this.cursor && !this.cursor_exec && this.length() < this.concurrency) {
    this.cursor_exec = true;
    this.cursor.nextObject(function(err, item) {
        console.log(this.name + ': ' + (item ? item._id : null) + ' ' + (err ? err : null));
        this.cursor_exec = false;
        if (item) {
            this.push(item);
        } else {
            delete this.cursor;
        }
    }.bind(this));
}

这篇关于Mongo的Cursor.nextObject有时错误地返回Null吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆