Mongodb 批量插入连接超时
Mongodb bulk Insert connection timeout
我有一个批量操作,我每 2 小时在我的 mongodb 数据库中插入 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)
}
}
我的连接总是超时。我的猫鼬连接选项如下所示
let options = {
mongos: {
ssl: true,
sslValidate: true,
sslCA: ca,
}
}
我很清楚其他 Whosebug 线程正在讨论的连接设置
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
}
}
}
好的,我已经将我的批量操作减少了 100 批而不是 1000 批,现在它工作正常..
当我做的时候它对 1000 很有效:
bulk.insert(programme);
但是现在,我这样做了,但它不适用于 1000/批量 :
bulk.find({ eventId: programme.eventId }).upsert().replaceOne(programme);
降低批处理对我帮助不大,反而增加了连接超时值。
这是我的连接字符串,它解决了我的问题。
MONGO_URI=mongodb://user:password@127.0.0.1:27017/dbname?keepAlive=true&poolSize=30&autoReconnect=true&socketTimeoutMS=360000&connectTimeoutMS=360000
我有一个批量操作,我每 2 小时在我的 mongodb 数据库中插入 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)
}
}
我的连接总是超时。我的猫鼬连接选项如下所示
let options = {
mongos: {
ssl: true,
sslValidate: true,
sslCA: ca,
}
}
我很清楚其他 Whosebug 线程正在讨论的连接设置
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
}
}
}
好的,我已经将我的批量操作减少了 100 批而不是 1000 批,现在它工作正常.. 当我做的时候它对 1000 很有效:
bulk.insert(programme);
但是现在,我这样做了,但它不适用于 1000/批量 :
bulk.find({ eventId: programme.eventId }).upsert().replaceOne(programme);
降低批处理对我帮助不大,反而增加了连接超时值。
这是我的连接字符串,它解决了我的问题。
MONGO_URI=mongodb://user:password@127.0.0.1:27017/dbname?keepAlive=true&poolSize=30&autoReconnect=true&socketTimeoutMS=360000&connectTimeoutMS=360000