Mongodb批量插入连接超时。

4

我有一个批量操作,在我的mongodb数据库中每两个小时upsert 10000个条目。代码如下:

let bulk = models.Product.collection.initializeUnorderedBulkOp();
...
    if (bulk.length > 0) {
        bulk.find({
            "$or": [
                {
                    "updatedAt": {
                        "$lt": timestamp
                    }
                },
                {
                    "discount": {
                        "$eq": 0
                    }
                }
            ]

        }).remove()
        bulk.execute((error, result) => {
            if (error) {
                console.error('Error while inserting products' + JSON.stringify(error))
            }
            else {
                console.log('Successfully inserted ' + result.nInserted + ' upserted ' + result.nUpserted + ' matched ' + result.nMatched + ' modified ' + result.nModified + ' removed ' + result.nRemoved)
            }
        })
    }
    else {
        console.log('There were no bulk operations to execute ' + products.length)
    }
}

我的连接一直超时。 我的mongoose连接选项如下所示

let options = {
    mongos: {
        ssl: true,
        sslValidate: true,
        sslCA: ca,
    }
}

我很清楚其他stackoverflow帖子中讨论的这个连接设置

server: {
    socketOptions: {
        keepAlive: 300000,
        connectTimeoutMS: 30000
    }
}

我阅读了keepAlive和connectTimeoutMS的文档,但我如何确定两者的正确值?我需要socketTimeoutMS吗?

提前感谢您的建议。

更新1

我一直收到这个错误:

 {"name":"MongoError","message":"connection 0 to aws-ap-southeast-1-portal.2.dblayer.com:15284 timed out"}

现在我的连接选项如下所示 //Compose.io数据库的选项

let options = {
    mongos: {
        ssl: true,
        sslValidate: true,
        sslCA: ca,
    },
    server: {
        socketOptions: {
            keepAlive: 300000,
            connectTimeoutMS: 300000
        }
    },
    replset: {
        socketOptions:
        {
            keepAlive: 300000,
            connectTimeoutMS: 300000
        }
    }
}
2个回答

4
降低批处理并没有帮助我解决问题,但是增加连接超时值确实有所帮助。
这是我的连接字符串,它为我解决了问题。
MONGO_URI=mongodb://user:password@127.0.0.1:27017/dbname?keepAlive=true&poolSize=30&autoReconnect=true&socketTimeoutMS=360000&connectTimeoutMS=360000

1

好的,我已经将批量操作降低到每批100个,而不是1000个,现在它可以正常工作了。当我进行以下操作时,它适用于1000个:

bulk.insert(programme);

但是现在,我正在做这件事,但对于1000 / 批次并不起作用:
bulk.find({ eventId: programme.eventId }).upsert().replaceOne(programme);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接