How to create and use client sessions.
Create a Client Session
Basic Usage
EXPECT(&session.client() == &client);
EXPECT(session.id()["id"]);
EXPECT(session.id()["id"].type() == bsoncxx::type::k_binary);
EXPECT(session.id()["id"].get_binary().sub_type == bsoncxx::binary_sub_type::k_uuid);
}
With Options
mongocxx::options::client_session opts;
opts.snapshot(true);
EXPECT(session.options().snapshot() == true);
}
Use a Client Session
- See also
-
Basic Usage
EXPECT(coll.insert_one(session, x1.view()));
EXPECT(doc_opt);
auto& doc = *doc_opt;
EXPECT(doc["x"]);
EXPECT(doc["x"].get_int32().value == 2);
}
With Transactions
session.start_transaction();
{
auto result_opt = coll.create_bulk_write(session)
.append(insert_one{x0.view()})
.append(insert_one{x0.view()})
.append(insert_one{x0.view()})
.execute();
EXPECT(result_opt);
EXPECT(result_opt->inserted_count() == 3);
}
{
auto result_opt =
EXPECT(result_opt);
EXPECT(result_opt->modified_count() == 3);
}
session.commit_transaction();
}