MongoDB 使用事务时出错:所有事务操作的总大小必须小于 16793600。实际大小为 16793810:一般服务器错误
MongoDB error using transactions: Total size of all transaction operations must be less than 16793600. Actual size is 16793810: generic server error
我正在使用 C++ 处理 mongodb 事务。我执行的步骤如下:
- 创建会话
- 为此会话创建一个 bulk_write,我有
多次插入集合
- 开始交易
- 执行bulk_write
- 提交交易
这是算法的代码片段:
#include <iostream>
#include <vector>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/exception/exception.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/exception/exception.hpp>
#include <mongocxx/exception/logic_error.hpp>
#include <mongocxx/exception/operation_exception.hpp>
int main(int argc, char** argv)
{
/* Parameters */
std::string db_uri = "<PROVIDE URI TO CONNECT WITH A MONGO DB WITH REPLICA SETS>";
std::string db_name = "db_0";
std::string collection0_name = "coll_0";
int N_INSERTS = 100000;
/* Init connection */
static mongocxx::instance inst{};
mongocxx::uri client_uri = mongocxx::uri(db_uri);
mongocxx::options::client client_options;
mongocxx::options::ssl ssl_options;
ssl_options.allow_invalid_certificates(true);
client_options.ssl_opts(ssl_options);
mongocxx::client client = mongocxx::client(client_uri, client_options);
/* Reinit collection */
mongocxx::database db = client[db_name];
auto builder = bsoncxx::builder::stream::document{};
bsoncxx::document::value doc_value = builder
<< "Hello" << "MongoDB"
<< bsoncxx::builder::stream::finalize;
db[collection0_name].insert_one(doc_value.view()); /* insert a dummy doc */
db[collection0_name].delete_many({}); /* delete all docs */
/* Create session */
mongocxx::client_session session = client.start_session();
/* Start transaction */
session.start_transaction();
/* Create bulk operations */
mongocxx::bulk_write op0 = db[collection0_name].create_bulk_write(session);
/* Fill insert bulk operations */
for (int i = 0; i < N_INSERTS; i++){
mongocxx::model::insert_one insert_one{
bsoncxx::builder::basic::make_document(
bsoncxx::builder::basic::kvp("field0", i),
bsoncxx::builder::basic::kvp("field1", i),
bsoncxx::builder::basic::kvp("field2", i)
)
};
op0.append(insert_one);
}
/* Execute transaction */
try {
bulk_op->execute();
}
catch (std::exception& e){
std::cerr << "Bulk write exception: " << e.what() << std::endl;
session.abort_transaction();
}
session.commit_transaction();
return 0;
}
您可以在安装了 mongocxx 的 linux 系统中使用以下命令进行编译:
c++ --std=c++11 test.cpp -o test -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/bsoncxx/v_noabi -L/usr/local/lib -lmongocxx -lbsoncxx
执行时出现以下错误:
Total size of all transaction operations must be less than 16793600. Actual size is 16793810: generic server error
但我只插入了 10 万个文档。
什么可能产生错误?
事务大小是指数据的大小(以字节为单位),而不是文档的数量。这是一个 16 MB 的限制。如果您的事务大小超过 16 MB 的限制,您将收到此错误。
Mongodb 4.2 版本事务大小比以前的版本大。最近我遇到了同样的问题,我通过升级 mongodb.
解决了这个问题
我正在使用 C++ 处理 mongodb 事务。我执行的步骤如下:
- 创建会话
- 为此会话创建一个 bulk_write,我有 多次插入集合
- 开始交易
- 执行bulk_write
- 提交交易
这是算法的代码片段:
#include <iostream>
#include <vector>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/exception/exception.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/exception/exception.hpp>
#include <mongocxx/exception/logic_error.hpp>
#include <mongocxx/exception/operation_exception.hpp>
int main(int argc, char** argv)
{
/* Parameters */
std::string db_uri = "<PROVIDE URI TO CONNECT WITH A MONGO DB WITH REPLICA SETS>";
std::string db_name = "db_0";
std::string collection0_name = "coll_0";
int N_INSERTS = 100000;
/* Init connection */
static mongocxx::instance inst{};
mongocxx::uri client_uri = mongocxx::uri(db_uri);
mongocxx::options::client client_options;
mongocxx::options::ssl ssl_options;
ssl_options.allow_invalid_certificates(true);
client_options.ssl_opts(ssl_options);
mongocxx::client client = mongocxx::client(client_uri, client_options);
/* Reinit collection */
mongocxx::database db = client[db_name];
auto builder = bsoncxx::builder::stream::document{};
bsoncxx::document::value doc_value = builder
<< "Hello" << "MongoDB"
<< bsoncxx::builder::stream::finalize;
db[collection0_name].insert_one(doc_value.view()); /* insert a dummy doc */
db[collection0_name].delete_many({}); /* delete all docs */
/* Create session */
mongocxx::client_session session = client.start_session();
/* Start transaction */
session.start_transaction();
/* Create bulk operations */
mongocxx::bulk_write op0 = db[collection0_name].create_bulk_write(session);
/* Fill insert bulk operations */
for (int i = 0; i < N_INSERTS; i++){
mongocxx::model::insert_one insert_one{
bsoncxx::builder::basic::make_document(
bsoncxx::builder::basic::kvp("field0", i),
bsoncxx::builder::basic::kvp("field1", i),
bsoncxx::builder::basic::kvp("field2", i)
)
};
op0.append(insert_one);
}
/* Execute transaction */
try {
bulk_op->execute();
}
catch (std::exception& e){
std::cerr << "Bulk write exception: " << e.what() << std::endl;
session.abort_transaction();
}
session.commit_transaction();
return 0;
}
您可以在安装了 mongocxx 的 linux 系统中使用以下命令进行编译:
c++ --std=c++11 test.cpp -o test -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/bsoncxx/v_noabi -L/usr/local/lib -lmongocxx -lbsoncxx
执行时出现以下错误:
Total size of all transaction operations must be less than 16793600. Actual size is 16793810: generic server error
但我只插入了 10 万个文档。
什么可能产生错误?
事务大小是指数据的大小(以字节为单位),而不是文档的数量。这是一个 16 MB 的限制。如果您的事务大小超过 16 MB 的限制,您将收到此错误。
Mongodb 4.2 版本事务大小比以前的版本大。最近我遇到了同样的问题,我通过升级 mongodb.
解决了这个问题