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