使用 mongocxx 3.5 在 mongo 4.0 和 4.2 上的不同行为
Different behaviour on mongo 4.0 and 4.2 using mongocxx 3.5
以下简单代码在 mongo 4.2.6 和 4.0
上表现出不同的行为
#include <iostream>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/instance.hpp>
using bsoncxx::builder::basic::make_document;
using bsoncxx::builder::basic::kvp;
int main(int, char**) {
std::cout << "Creating instance" << std::endl;
mongocxx::instance instance{};
auto uri = mongocxx::uri{mongocxx::uri::k_default_uri};
std::cout << "Creating client" << std::endl;
auto client = mongocxx::client{uri};
std::cout << "Accessing DB " << std::endl;
auto db = client["mydb"];
auto coll = db["my_collection"];
auto criteria = make_document(kvp("x", "foo"));
auto update = make_document(kvp("$set", make_document(kvp("x", "bar"))));
auto write_concern = mongocxx::write_concern{};
write_concern.journal(true);
write_concern.acknowledge_level(
mongocxx::write_concern::level::k_majority);
std::cout << "Setting options" << std::endl;
auto options = mongocxx::options::find_one_and_update()
.write_concern(std::move(write_concern))
.return_document(mongocxx::options::return_document::k_before);
std::cout << "Invoking find_one_and_update" << std::endl;
coll.find_one_and_update(
criteria.view(),
update.view(),
options);
std::cout << "Done" << std::endl;
}
在 mongo 4.0 上,一切按预期工作并给出此输出,
Creating client
Accessing DB
Setting options
Invoking find_one_and_update
Done
但是,当我 运行 针对 mongo 4.2.6 服务器时,我看到了这个,
Creating client
Accessing DB
Setting options
Invoking find_one_and_update
terminate called after throwing an instance of 'mongocxx::v_noabi::write_exception'
what(): BSON field 'j' is an unknown field.: generic server error
Aborted
这个j
好像来自write_concern.journal(true);
行。这是 mongo 4.2.6 日志对此的描述,
1405 2020-05-24T21:57:55.798+0000 D2 COMMAND [conn2] run command mydb.$cmd { findAndModify: "my_collection", query: { x: "foo" }, update: { $set: { x: "bar" } }, w: "majority", j: true, $db: "mydb", lsid : { id: UUID("a2067505-a443-4427-84a6-500f83ce310c") } }
1406 2020-05-24T21:57:55.799+0000 D1 - [conn2] User Assertion: Location51177: BSON field 'j' is an unknown field. src/mongo/db/commands/find_and_modify.cpp 315
1407 2020-05-24T21:57:55.799+0000 D1 COMMAND [conn2] assertion while executing command 'findAndModify' on database 'mydb' with arguments '{ findAndModify: "my_collection", query: { x: "foo" }, update: { $set: { x: "bar" } }, w: "majority", j: true, $db: "mydb", lsid: { id: UUID("a2067505-a443-4427-84a6-500f83ce310c") } }': Location51177: BSON field 'j' is an unknown field.
1408 2020-05-24T21:57:55.799+0000 I COMMAND [conn2] command mydb.$cmd command: findAndModify { findAndModify: "my_collection", query: { x: "foo" }, update: { $set: { x: "bar" } }, w: "majority", j: true , $db: "mydb", lsid: { id: UUID("a2067505-a443-4427-84a6-500f83ce310c") } } numYields:0 ok:0 errMsg:"BSON field 'j' is an unknown field." errName:Location51177 errCode:51177 reslen:124 locks:{} proto col:op_msg 0ms
我打算在 mongocxx JIRA 项目中为此创建一个错误。将此张贴在这里,以便让更多人看到此内容 - 以防我忽略某些内容。
详情,
- Mongo 4.2.6 取自:http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.6.tgz
- Mongo 4.0.0 取自:http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.0.tgz
- Mongo-cxx 是从以下位置获取和构建的:https://github.com/mongodb/mongo-cxx-driver/archive/r3.5.0.tar.gz
在这两种情况下,mongo 都是用这样的命令启动的,
mongod --port=27017 --bind_ip_all -vvvvv --fork \
--dbpath=/my/db/path/ --logpath=/my/db/path/logs/mongod.log
我们已经能够验证这是 mongocxx 驱动程序中的错误。已在此处记录:https://jira.mongodb.org/browse/CXX-2028
以下简单代码在 mongo 4.2.6 和 4.0
上表现出不同的行为#include <iostream>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/instance.hpp>
using bsoncxx::builder::basic::make_document;
using bsoncxx::builder::basic::kvp;
int main(int, char**) {
std::cout << "Creating instance" << std::endl;
mongocxx::instance instance{};
auto uri = mongocxx::uri{mongocxx::uri::k_default_uri};
std::cout << "Creating client" << std::endl;
auto client = mongocxx::client{uri};
std::cout << "Accessing DB " << std::endl;
auto db = client["mydb"];
auto coll = db["my_collection"];
auto criteria = make_document(kvp("x", "foo"));
auto update = make_document(kvp("$set", make_document(kvp("x", "bar"))));
auto write_concern = mongocxx::write_concern{};
write_concern.journal(true);
write_concern.acknowledge_level(
mongocxx::write_concern::level::k_majority);
std::cout << "Setting options" << std::endl;
auto options = mongocxx::options::find_one_and_update()
.write_concern(std::move(write_concern))
.return_document(mongocxx::options::return_document::k_before);
std::cout << "Invoking find_one_and_update" << std::endl;
coll.find_one_and_update(
criteria.view(),
update.view(),
options);
std::cout << "Done" << std::endl;
}
在 mongo 4.0 上,一切按预期工作并给出此输出,
Creating client
Accessing DB
Setting options
Invoking find_one_and_update
Done
但是,当我 运行 针对 mongo 4.2.6 服务器时,我看到了这个,
Creating client
Accessing DB
Setting options
Invoking find_one_and_update
terminate called after throwing an instance of 'mongocxx::v_noabi::write_exception'
what(): BSON field 'j' is an unknown field.: generic server error
Aborted
这个j
好像来自write_concern.journal(true);
行。这是 mongo 4.2.6 日志对此的描述,
1405 2020-05-24T21:57:55.798+0000 D2 COMMAND [conn2] run command mydb.$cmd { findAndModify: "my_collection", query: { x: "foo" }, update: { $set: { x: "bar" } }, w: "majority", j: true, $db: "mydb", lsid : { id: UUID("a2067505-a443-4427-84a6-500f83ce310c") } }
1406 2020-05-24T21:57:55.799+0000 D1 - [conn2] User Assertion: Location51177: BSON field 'j' is an unknown field. src/mongo/db/commands/find_and_modify.cpp 315
1407 2020-05-24T21:57:55.799+0000 D1 COMMAND [conn2] assertion while executing command 'findAndModify' on database 'mydb' with arguments '{ findAndModify: "my_collection", query: { x: "foo" }, update: { $set: { x: "bar" } }, w: "majority", j: true, $db: "mydb", lsid: { id: UUID("a2067505-a443-4427-84a6-500f83ce310c") } }': Location51177: BSON field 'j' is an unknown field.
1408 2020-05-24T21:57:55.799+0000 I COMMAND [conn2] command mydb.$cmd command: findAndModify { findAndModify: "my_collection", query: { x: "foo" }, update: { $set: { x: "bar" } }, w: "majority", j: true , $db: "mydb", lsid: { id: UUID("a2067505-a443-4427-84a6-500f83ce310c") } } numYields:0 ok:0 errMsg:"BSON field 'j' is an unknown field." errName:Location51177 errCode:51177 reslen:124 locks:{} proto col:op_msg 0ms
我打算在 mongocxx JIRA 项目中为此创建一个错误。将此张贴在这里,以便让更多人看到此内容 - 以防我忽略某些内容。
详情,
- Mongo 4.2.6 取自:http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.6.tgz
- Mongo 4.0.0 取自:http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.0.tgz
- Mongo-cxx 是从以下位置获取和构建的:https://github.com/mongodb/mongo-cxx-driver/archive/r3.5.0.tar.gz
在这两种情况下,mongo 都是用这样的命令启动的,
mongod --port=27017 --bind_ip_all -vvvvv --fork \
--dbpath=/my/db/path/ --logpath=/my/db/path/logs/mongod.log
我们已经能够验证这是 mongocxx 驱动程序中的错误。已在此处记录:https://jira.mongodb.org/browse/CXX-2028