1MONGOC_SESSION_OPTS_SET_SNAPSHOT(3)libmongocMONGOC_SESSION_OPTS_SET_SNAPSHOT(3)
2
3
4
6 mongoc_session_opts_set_snapshot - mongoc_session_opts_set_snapshot()
7
9 void
10 mongoc_session_opts_set_snapshot (mongoc_session_opt_t *opts,
11 bool snapshot);
12
13 Configure snapshot reads for a session. If true (false by default),
14 each read operation in the session will be sent with a "snapshot" level
15 read concern. After the first read operation ("find", "aggregate" or
16 "distinct"), subsequent read operations will read from the same point
17 in time as the first read operation. Set to true to enable snapshot
18 reads. See the official documentation for Read Concern "snapshot".
19
20 Snapshot reads and causal consistency are mutually exclusive. Attempt‐
21 ing to set both to true will result in an error. See
22 mongoc_session_opts_set_causal_consistency().
23
24 Snapshot reads can only be used on MongoDB server version 5.0 and later
25 and cannot be used during a transaction. A write operation in a snap‐
26 shot-enabled session will also result in an error.
27
29 • opts: A mongoc_session_opt_t.
30
31 • snapshot: True or false.
32
34 mongoc_client_t *client;
35 mongoc_session_opt_t *session_opts;
36 mongoc_client_session_t *client_session;
37 mongoc_collection_t *collection;
38 bson_t query_opts = BSON_INITIALIZER;
39 bson_t filter = BSON_INITIALIZER;
40 bson_t pipeline = BSON_INITIALIZER;
41
42 client = mongoc_client_new ("mongodb://example/?appname=session-opts-example");
43 mongoc_client_set_error_api (client, MONGOC_ERROR_API_VERSION_2);
44
45 session_opts = mongoc_session_opts_new ();
46 mongoc_session_opts_set_snapshot (session_opts, true);
47 client_session = mongoc_client_start_session (client, session_opts, &error);
48 mongoc_session_opts_destroy (session_opts);
49
50 if (!client_session) {
51 fprintf (stderr, "Failed to start session: %s\n", error.message);
52 abort ();
53 }
54
55 collection = mongoc_client_get_collection (client, "test", "collection");
56 r = mongoc_client_session_append (client_session, &find_opts, &error);
57 if (!r) {
58 fprintf (stderr, "mongoc_client_session_append failed: %s\n", error.message);
59 abort ();
60 }
61
62 /* First read operation will set the snapshot time for subsequent reads. */
63 cursor = mongoc_collection_find_with_opts (collection, filter, &query_opts, NULL);
64
65 /* Subsequent read operations will automatically read from the same point
66 * in time as the first read operation. */
67 cursor = mongoc_collection_aggregate (collection, MONGOC_QUERY_NONE, pipeline, &query_opts, NULL);
68
70 MongoDB, Inc
71
73 2017-present, MongoDB, Inc
74
75
76
77
781.23.1 Oct 20, 202M2ONGOC_SESSION_OPTS_SET_SNAPSHOT(3)