MongoDB 对多个集合执行事务时出现 Atlas 错误(代码 8000)

MongoDB Atlas Error while performing transaction on multiple collections (code 8000)

我正在尝试从 Mongo DB Node JS 驱动程序(如 here 所述)在 Mongo DB Atlas M0 实例上执行事务,我得到以下信息错误:

code:8000
codeName:"AtlasError"
errmsg:"internal atlas error checking things: Failure getting dbStats: read tcp 192.168.254.78:50064->192.168.254.78:27000: i/o timeout"
message:"internal atlas error checking things: Failure getting dbStats: read tcp 192.168.254.78:50064->192.168.254.78:27000: i/o timeout"
name:"MongoError"

我已经搜索了一段时间,但找不到解决此问题的任何线索。

补充信息:

我的代码:

async function connect () {

if (dbClient !== null && dbClient.isConnected()) {
    console.log('Reusing db connection => ' + JSON.stringify(dbClient));
  } else {
      console.log('Connecting to database');
      dbClient = await MongoClient.connect(url, { useNewUrlParser: true });
      console.log('Successfully connected to database');
  }
}

async function insertDocuments(document1, document2, document3) {

  try {
    await connect();
  } catch (error) {
    throw error
  }

  let session = dbClient.startSession();

  session.startTransaction({
    readConcern: { level: 'snapshot' },
    writeConcern: { w: 'majority' }
  });

  const collection1 = dbClient.db('mydbname').collection('collection1');
  const collection2 = dbClient.db('mydbname').collection('collection2');
  const collection3 = dbClient.db('mydbname').collection('collection3');
  const logsCollection = dbClient.db('mydbname').collection('logs');

  await collection1.replaceOne(
    { _id: document1._id },
    document1,
    {
      upsert: true,
      session
    }
  );
  await collection2.replaceOne(
    { _id: document2._id },
    document2,
    {
      upsert: true,
      session
    }
  );
  await collection3.replaceOne(
    { _id: document3._id },
    document3,
    {
      upsert: true,
      session
    }
  );
  await logsCollection.updateOne(
    { _id: document1._id },
    { $unset: { estoque: '' } },
    { session }
  );

  try {
    await commitWithRetry(session);
  } catch (error) {
    await session.abortTransaction();
    throw error;
  }
}

async function commitWithRetry(session) {
  try {
    await session.commitTransaction();
    console.log('Transação gravada com sucesso');
  } catch (error) {
    if (
      error.errorLabels &&
      error.errorLabels.indexOf('UnknownTransactionCommitResult') >= 0
    ) {
      console.log('Transação não realizada, tentando novamente ...');
      await commitWithRetry(session);
    } else {
      console.log('Erro ao gravar no banco de dados ...');
      throw error;
    }
  }
}

知道如何解决这个问题吗?提前致谢!

我遇到了同样的问题。支持部门说,既然我用的是 Mongoose,他们帮不上忙。但是,他们非常友好地赠送了我 10 美元的信用额度,这样我就可以在更高级别进行测试。

这就是问题所在,一旦我升级到 M10 层(M0 之后的下一个)交易就开始按预期工作。您的代码与我的代码非常相似,只是我创建了一个文档并同时更新了另外两个文档。就像您的情况一样,第一个通过(无论顺序如何)而下一个只是超时并出现相同的错误。

我遇到了同样的问题,但无法解决。我最终在 Azure(而不是 AWS)上创建了一个新集群,它似乎运行良好。

不确定这是他们在 AWS 上的实施问题还是个案错误配置

更新: 新集群现在出现同样的错误。我提出了一个问题并得到了以下回复;

This has been determined to be a bug currently affecting the M0 free cluster and shared tiers of Atlas (M2 and M5). We have opened an internal bug report to address this. While the internal development queue for our Cloud products is not publicly visible, we are tracking the work required to make multi-document transactions work on Atlas free tier clusters.

他们说的和另一个答案一样。请使用 M10。然而,M10 约为 60 美元/米,这无论如何都不是一个很好的解决方案。这也完全使他们说 M0 现在包括 MongoDb 4.0

的主要卖点无效

如评论中所述,根据 MongoDB 团队的说法,此问题已在版本 4.0.5

中得到解决

我已经测试了发布在我的问题上的相同代码,现在它工作正常。

对于遇到相同问题的任何人,请确保使用等于或高于 4.0.5 的版本。

第一步下载https://robomongo.org/download

  • 当您拥有 Studio 3T 试用许可证界面时,请转到快速入门

第 2 步:创建新连接。确保端口是 localhost:27017 并且 db name 是你的项目名称

第 3 步:您将看到一个蓝色的连接,请注意保持默认。

  • 您将收到初始错误,因为数据库未安装在终端上。

转到:https://docs.mongodb.com/manual/tutorial/install-mongodb-on-os-x/

在 macOS 的终端项目文件夹中。

  1. brew tap mongodb/brew
  2. brew install mongodb-community@4.2
  3. mongo。 如果你得到 ==> Successfully started `mongodb-community` (label: homebrew.mxcl.mongodb-community),那么希望终端没有错误。

最后一步:返回界面点击蓝色按钮DB会自动连接

// .env file on project root folder
MONGO_URI='mongodb://localhost:27017/projectname'

连接代码如下:

const express = require('express');
const mongoose = require('mongoose');

require('dotenv').config();

const app = express();

mongoose
  .connect(process.env.MONGO_URI, { useNewUrlParser: true })
  .then(() => console.log('DB Connected'));

mongoose.connection.on('error', (err) => {
  console.log(`DB connection error: ${err.message}`);
});

app.get('/', (req, res) => {
  res.send('hello from the other side');
});

我遇到了类似的错误,我花了几个小时尝试调试它。然后我删除了拥有 AWS 的旧集群并使用 Google Cloud 创建了一个新集群,并且运行良好。

我遇到过类似的问题,请检查您的连接 URI。我的密码有误。

我也遇到过这种类型的错误。作为结论,设置您的集群密码时不要使用“@/#”等特殊字符。或者使用自动生成的密码。它对我有用

我通过在 MongoDb 站点上创建一个带有 atlas 的数据库解决了这个问题。

我通过在 mongo db Cloub 中转到我的数据库中的网络访问并删除旧的 IP 地址并通过单击添加 IP 地址并选择当前 ID 来放置新的 IP 地址,从而解决了这个问题地址

我也遇到了同样的问题,在我的情况下,我使用了一个星期的临时用户,但我忘记了它。所以也许它可以帮助某人:)