1
+ // example-bulkwrite shows use of `mongoc_client_bulkwrite`.
2
+
3
+ #include <mongoc/mongoc.h>
4
+
5
+ #define HANDLE_ERROR (...) \
6
+ if (1) { \
7
+ fprintf (stderr, __VA_ARGS__); \
8
+ fprintf (stderr, "\n"); \
9
+ goto fail; \
10
+ } else \
11
+ (void) 0
12
+
13
+ int
14
+ main (int argc , char * argv [])
15
+ {
16
+ bool ok = false;
17
+
18
+ mongoc_init ();
19
+
20
+ bson_error_t error ;
21
+ mongoc_client_t * client = mongoc_client_new ("mongodb://localhost:27017" );
22
+ mongoc_bulkwriteopts_t * bwo = mongoc_bulkwriteopts_new ();
23
+ mongoc_bulkwriteopts_set_verboseresults (bwo , true);
24
+ mongoc_bulkwrite_t * bw = mongoc_client_bulkwrite_new (client );
25
+
26
+ // Insert a document to `db.coll1`
27
+ {
28
+ bson_t * doc = BCON_NEW ("foo" , "bar" );
29
+ if (!mongoc_bulkwrite_append_insertone (bw , "db.coll1" , doc , NULL , & error )) {
30
+ HANDLE_ERROR ("Error appending insert one: %s" , error .message );
31
+ }
32
+ bson_destroy (doc );
33
+ }
34
+ // Insert a document to `db.coll2`
35
+ {
36
+ bson_t * doc = BCON_NEW ("foo" , "baz" );
37
+ if (!mongoc_bulkwrite_append_insertone (bw , "db.coll2" , doc , NULL , & error )) {
38
+ HANDLE_ERROR ("Error appending insert one: %s" , error .message );
39
+ }
40
+ bson_destroy (doc );
41
+ }
42
+
43
+ mongoc_bulkwritereturn_t bwr = mongoc_bulkwrite_execute (bw , bwo );
44
+
45
+ // Print results.
46
+ {
47
+ BSON_ASSERT (bwr .res ); // Has results. NULL only returned for unacknowledged writes.
48
+ printf ("Insert count : %" PRId64 "\n" , mongoc_bulkwriteresult_insertedcount (bwr .res ));
49
+ const bson_t * ir = mongoc_bulkwriteresult_insertresults (bwr .res );
50
+ BSON_ASSERT (ir ); // Has verbose results. NULL only returned if verbose results not requested.
51
+ char * ir_str = bson_as_relaxed_extended_json (ir , NULL );
52
+ printf ("Insert results : %s\n" , ir_str );
53
+ bson_free (ir_str );
54
+ }
55
+
56
+ // Print all error information. To observe: try setting the `_id` fields to cause a duplicate key error.
57
+ if (bwr .exc ) {
58
+ const char * msg = "(none)" ;
59
+ if (mongoc_bulkwriteexception_error (bwr .exc , & error )) {
60
+ msg = error .message ;
61
+ }
62
+ const bson_t * we = mongoc_bulkwriteexception_writeerrors (bwr .exc );
63
+ char * we_str = bson_as_relaxed_extended_json (we , NULL );
64
+ const bson_t * wce = mongoc_bulkwriteexception_writeconcernerrors (bwr .exc );
65
+ char * wce_str = bson_as_relaxed_extended_json (wce , NULL );
66
+ const bson_t * er = mongoc_bulkwriteexception_errorreply (bwr .exc );
67
+ char * er_str = bson_as_relaxed_extended_json (er , NULL );
68
+ printf ("Top-level error : %s\n" , msg );
69
+ printf ("Write errors : %s\n" , we_str );
70
+ printf ("Write concern errors : %s\n" , wce_str );
71
+ printf ("Error reply : %s\n" , er_str );
72
+ bson_free (er_str );
73
+ bson_free (wce_str );
74
+ bson_free (we_str );
75
+ }
76
+
77
+ mongoc_bulkwriteresult_destroy (bwr .res );
78
+ mongoc_bulkwriteexception_destroy (bwr .exc );
79
+ mongoc_bulkwrite_destroy (bw );
80
+
81
+ ok = true;
82
+ fail :
83
+ mongoc_client_destroy (client );
84
+ mongoc_bulkwriteopts_destroy (bwo );
85
+ mongoc_cleanup ();
86
+ return ok ? EXIT_SUCCESS : EXIT_FAILURE ;
87
+ }
0 commit comments