Skip to content

Instantly share code, notes, and snippets.

@ajdavis
Created May 28, 2015 11:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajdavis/62d729c7e3739cfe331e to your computer and use it in GitHub Desktop.
Save ajdavis/62d729c7e3739cfe331e to your computer and use it in GitHub Desktop.
Example mongorestore-type program with the Mongo C Driver
#include <bson.h>
#include <mongoc.h>
static int execute (mongoc_bulk_operation_t *bulk) {
bson_t reply;
bson_error_t error;
int ret;
char *str;
ret = mongoc_bulk_operation_execute (bulk, &reply, &error);
if (!ret) {
fprintf (stderr, "%s\n", error.message);
}
str = bson_as_json (&reply, NULL);
fprintf (stderr, "%s\n", str);
bson_free (str);
return ret ? EXIT_SUCCESS : EXIT_FAILURE;
}
static int
mongoc_restore (mongoc_client_t *client,
const char *database,
const char *collection,
const char *filename)
{
mongoc_collection_t *col;
bson_reader_t *reader;
bson_error_t error;
mongoc_bulk_operation_t *bulk;
const bson_t *b;
int i;
int ret = EXIT_SUCCESS;
col = mongoc_client_get_collection (client,
database,
collection);
assert (col);
reader = bson_reader_new_from_file (filename, &error);
if (!reader) {
fprintf (stderr, "%s %s\n", filename, error.message);
return EXIT_FAILURE;
}
/* could pass ordered "false" instead to continue past errs */
bulk = mongoc_collection_create_bulk_operation (col, true, NULL);
i = 0;
while ((b = bson_reader_read (reader, NULL))) {
mongoc_bulk_operation_insert (bulk, b);
i++;
/* insert 1000 docs at a time */
if (i == 1000) {
ret = execute (bulk);
if (ret != EXIT_SUCCESS) {
/* could choose instead to continue past errs */
goto fail;
}
mongoc_bulk_operation_destroy (bulk);
bulk = mongoc_collection_create_bulk_operation (col, true, NULL);
i = 0;
}
}
if (i) {
ret = execute (bulk);
}
fail:
mongoc_bulk_operation_destroy (bulk);
mongoc_collection_destroy (col);
bson_reader_destroy (reader);
return ret;
}
static void
usage (FILE *stream)
{
fprintf (stream,
"Usage: mongoc-restore [OPTIONS] DBNAME COLNAME FILE\n"
"\n"
"Load a bson dump file into MongoDB\n"
"\n"
"Options:\n"
"\n"
" -u URI Optional MongoDB URI to connect to [mongodb://localhost].\n"
"\n");
}
int
main (int argc,
char *argv[])
{
mongoc_client_t *client;
const char *collection;
const char *database;
char *uri = NULL;
int ret;
int arg_offset = 1;
const char *filename;
mongoc_init ();
if (argc < 2) {
usage (stderr);
return EXIT_FAILURE;
}
if (0 == strcmp (argv [1], "--help")) {
usage (stdout);
return EXIT_SUCCESS;
} else if (0 == strcmp (argv [1], "-u")) {
if (argc < 3) {
usage (stderr);
return EXIT_FAILURE;
}
uri = argv [2];
arg_offset = 3;
}
if (argc - arg_offset < 3) {
usage (stderr);
return EXIT_FAILURE;
}
database = argv[arg_offset];
collection = argv[arg_offset + 1];
filename = argv[arg_offset + 2];
if (!(client = mongoc_client_new (uri))) {
fprintf (stderr, "Invalid connection URI: %s\n", uri);
return EXIT_FAILURE;
}
ret = mongoc_restore (client, database, collection, filename);
mongoc_client_destroy (client);
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment