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 ⚠️ Any mongoc_cursor_t or mongoc_change_stream_t that's using a session must be destroyed before the session. And a session must be destroyed before the mongoc_client_t it 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 πŸ‘‘

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