MONGOC_SESSION_OPT_T(3)
π MONGOC_SESSION_OPT_T π
okay bestie, so this is a page from libmongoc β that's the MongoDB C driver manual, and today's main character is mongoc_session_opt_t. it's the little options struct you build before you start a session, so you can tell the session how to behave. she's iconic, she's tiny, she's a typedef:
#include <mongoc/mongoc.h>
typedef struct _mongoc_session_opt_t mongoc_session_opt_t;
like, that's literally the whole girl. moving on β¨
β¨ SYNOPSIS β¨
so here's the whole lifecycle, in order, no skipping steps π«Ά β you start a session with mongoc_client_start_session(), you use that session for a sequence of operations and for multi-document transactions, and then you free it with mongoc_client_session_destroy().
β οΈ destroy order matters, bestie, do NOT freestyle this β οΈ Anymongoc_cursor_tormongoc_change_stream_tthat's using a session must be destroyed before the session. And a session must be destroyed before themongoc_client_tit came from. yes, it's a whole stack of vibes, respect the stack π
causal consistency, the default era β by default, sessions are causally consistent. that's just how they come out the box π
if you wanna disable causal consistency, here's the lil ritual: before starting a session, create a mongoc_session_opt_t with mongoc_session_opts_new() and call mongoc_session_opts_set_causal_consistency(), then free the struct with mongoc_session_opts_destroy(). that's it. slay.
unacknowledged writes β they are prohibited with sessions. not "discouraged", not "frowned upon", prohibited. no cap π«
threading β a mongoc_client_session_t must be used by only one thread at a time. one. single. thread. she's not built for sharing π
the pooling thing (important!!) β due to session pooling, mongoc_client_start_session() may hand you back a session that has been idle for some time and is about to be closed after its idle timeout. so use the session within one minute of acquiring it, which refreshes the session and avoids a timeout. like a snack with a best-before date, lowkey π
btw, go look at the example code for mongoc_session_opts_set_causal_consistency() β iykyk, it makes this all click π
πΈ AUTHOR πΈ
MongoDB, Inc β the whole entire author, yes ma'am π
π COPYRIGHT π
2009-present, MongoDB, Inc. β that's the copyright line, keeping it exactly as it is because we don't mess with legal text fr fr π
okay that's the page!! libmongoc 2.5.6, dated Oct 09, 2026 β that's the source/version and date straight off the footer, mwah π go forth and destroy your sessions in the right order bestie, ily, bye!! π¦β¨π
π¦ where this page comes from
- epel 10: mongo-c-driver-devel 2.5.6-1.el10_3 Β· Apache-2.0 AND ISC AND MIT AND Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms