Skip to content

Commit 82b2d07

Browse files
author
Christian Hergert
committed
examples: add bulk operation examples.
1 parent c5074d3 commit 82b2d07

File tree

5 files changed

+243
-0
lines changed

5 files changed

+243
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
aclocal.m4
44
AUTHORS
55
autom4te.cache
6+
bulk1
7+
bulk2
8+
bulk3
69
ChangeLog
710
cluster1
811
*.cmake

examples/Makefile.am

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,18 @@ noinst_PROGRAMS += example-matcher
5050
example_matcher_SOURCES = examples/example-matcher.c
5151
example_matcher_CFLAGS = $(EXAMPLE_CFLAGS)
5252
example_matcher_LDADD = $(EXAMPLE_LDADD)
53+
54+
noinst_PROGRAMS += bulk1
55+
bulk1_SOURCES = examples/bulk/bulk1.c
56+
bulk1_CFLAGS = $(EXAMPLE_CFLAGS)
57+
bulk1_LDADD = $(EXAMPLE_LDADD)
58+
59+
noinst_PROGRAMS += bulk2
60+
bulk2_SOURCES = examples/bulk/bulk2.c
61+
bulk2_CFLAGS = $(EXAMPLE_CFLAGS)
62+
bulk2_LDADD = $(EXAMPLE_LDADD)
63+
64+
noinst_PROGRAMS += bulk3
65+
bulk3_SOURCES = examples/bulk/bulk3.c
66+
bulk3_CFLAGS = $(EXAMPLE_CFLAGS)
67+
bulk3_LDADD = $(EXAMPLE_LDADD)

examples/bulk/bulk1.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <assert.h>
2+
#include <bcon.h>
3+
#include <mongoc.h>
4+
#include <stdio.h>
5+
6+
static void
7+
bulk1 (mongoc_collection_t *collection)
8+
{
9+
mongoc_bulk_operation_t *bulk;
10+
bson_error_t error;
11+
bson_oid_t oid;
12+
bson_t *doc;
13+
bson_t reply;
14+
char *str;
15+
bool ret;
16+
int i;
17+
18+
bulk = mongoc_collection_create_bulk_operation (collection, true, NULL);
19+
20+
for (i = 0; i < 10000; i++) {
21+
bson_oid_init (&oid, NULL);
22+
doc = BCON_NEW ("_id", BCON_OID (&oid),
23+
"hello", BCON_UTF8 ("world"),
24+
"i", BCON_INT32 (i));
25+
mongoc_bulk_operation_insert (bulk, doc);
26+
bson_destroy (doc);
27+
}
28+
29+
ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
30+
31+
str = bson_as_json (&reply, NULL);
32+
printf ("%s\n", str);
33+
bson_free (str);
34+
bson_destroy (&reply);
35+
36+
if (!ret) {
37+
fprintf (stderr, "Error: %s\n", error);
38+
}
39+
}
40+
41+
int
42+
main (int argc,
43+
char *argv[])
44+
{
45+
mongoc_client_t *client;
46+
mongoc_collection_t *collection;
47+
48+
mongoc_init ();
49+
50+
client = mongoc_client_new ("mongodb://localhost/");
51+
collection = mongoc_client_get_collection (client, "test", "test");
52+
53+
bulk1 (collection);
54+
55+
mongoc_collection_destroy (collection);
56+
mongoc_client_destroy (client);
57+
58+
mongoc_cleanup ();
59+
60+
return 0;
61+
}

examples/bulk/bulk2.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <assert.h>
2+
#include <bcon.h>
3+
#include <mongoc.h>
4+
#include <stdio.h>
5+
6+
static void
7+
bulk2 (mongoc_collection_t *collection)
8+
{
9+
mongoc_bulk_operation_t *bulk;
10+
bson_error_t error;
11+
bson_t *query;
12+
bson_t *doc;
13+
bson_t reply;
14+
char *str;
15+
bool ret;
16+
int i;
17+
18+
bulk = mongoc_collection_create_bulk_operation (collection, true, NULL);
19+
20+
/* Remove everything */
21+
query = bson_new ();
22+
mongoc_bulk_operation_remove (bulk, query);
23+
bson_destroy (query);
24+
25+
/* Add a few documents */
26+
for (i = 1; i < 4; i++) {
27+
doc = BCON_NEW ("_id", BCON_INT32 (i));
28+
mongoc_bulk_operation_insert (bulk, doc);
29+
bson_destroy (doc);
30+
}
31+
32+
/* {_id: 1} => {$set: {foo: "bar"}} */
33+
query = BCON_NEW ("_id", BCON_INT32 (1));
34+
doc = BCON_NEW ("$set", "{", "foo", BCON_UTF8 ("bar"), "}");
35+
mongoc_bulk_operation_update (bulk, query, doc, false);
36+
bson_destroy (query);
37+
bson_destroy (doc);
38+
39+
/* {_id: 4} => {'$inc': {'j': 1}} (upsert) */
40+
query = BCON_NEW ("_id", BCON_INT32 (4));
41+
doc = BCON_NEW ("$inc", "{", "j", BCON_INT32 (1), "}");
42+
mongoc_bulk_operation_update (bulk, query, doc, true);
43+
bson_destroy (query);
44+
bson_destroy (doc);
45+
46+
/* replace {j:1} with {j:2} */
47+
query = BCON_NEW ("j", BCON_INT32 (1));
48+
doc = BCON_NEW ("j", BCON_INT32 (2));
49+
mongoc_bulk_operation_replace_one (bulk, query, doc, false);
50+
bson_destroy (query);
51+
bson_destroy (doc);
52+
53+
ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
54+
55+
str = bson_as_json (&reply, NULL);
56+
printf ("%s\n", str);
57+
bson_free (str);
58+
59+
if (!ret) {
60+
printf ("Error: %s\n", error.message);
61+
}
62+
63+
bson_destroy (&reply);
64+
mongoc_bulk_operation_destroy (bulk);
65+
}
66+
67+
int
68+
main (int argc,
69+
char *argv[])
70+
{
71+
mongoc_client_t *client;
72+
mongoc_collection_t *collection;
73+
74+
mongoc_init ();
75+
76+
client = mongoc_client_new ("mongodb://localhost/");
77+
collection = mongoc_client_get_collection (client, "test", "test");
78+
79+
bulk2 (collection);
80+
81+
mongoc_collection_destroy (collection);
82+
mongoc_client_destroy (client);
83+
84+
mongoc_cleanup ();
85+
86+
return 0;
87+
}

examples/bulk/bulk3.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <assert.h>
2+
#include <bcon.h>
3+
#include <mongoc.h>
4+
#include <stdio.h>
5+
6+
static void
7+
bulk3 (mongoc_collection_t *collection)
8+
{
9+
mongoc_bulk_operation_t *bulk;
10+
bson_error_t error;
11+
bson_t *query;
12+
bson_t *doc;
13+
bson_t reply;
14+
char *str;
15+
bool ret;
16+
int i;
17+
18+
/* false indicates unordered */
19+
bulk = mongoc_collection_create_bulk_operation (collection, false, NULL);
20+
21+
/* Add a document */
22+
doc = BCON_NEW ("_id", BCON_INT32 (1));
23+
mongoc_bulk_operation_insert (bulk, doc);
24+
bson_destroy (doc);
25+
26+
/* remove {_id: 2} */
27+
query = BCON_NEW ("_id", BCON_INT32 (2));
28+
mongoc_bulk_operation_remove_one (bulk, query);
29+
bson_destroy (query);
30+
31+
/* insert {_id: 3} */
32+
doc = BCON_NEW ("_id", BCON_INT32 (3));
33+
mongoc_bulk_operation_insert (bulk, doc);
34+
bson_destroy (doc);
35+
36+
/* replace {_id:4} {'i': 1} */
37+
query = BCON_NEW ("_id", BCON_INT32 (4));
38+
doc = BCON_NEW ("i", BCON_INT32 (1));
39+
mongoc_bulk_operation_replace_one (bulk, query, doc, false);
40+
bson_destroy (query);
41+
bson_destroy (doc);
42+
43+
ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
44+
45+
str = bson_as_json (&reply, NULL);
46+
printf ("%s\n", str);
47+
bson_free (str);
48+
49+
if (!ret) {
50+
printf ("Error: %s\n", error.message);
51+
}
52+
53+
bson_destroy (&reply);
54+
mongoc_bulk_operation_destroy (bulk);
55+
}
56+
57+
int
58+
main (int argc,
59+
char *argv[])
60+
{
61+
mongoc_client_t *client;
62+
mongoc_collection_t *collection;
63+
64+
mongoc_init ();
65+
66+
client = mongoc_client_new ("mongodb://localhost/");
67+
collection = mongoc_client_get_collection (client, "test", "test");
68+
69+
bulk3 (collection);
70+
71+
mongoc_collection_destroy (collection);
72+
mongoc_client_destroy (client);
73+
74+
mongoc_cleanup ();
75+
76+
return 0;
77+
}

0 commit comments

Comments
 (0)