diff --git a/code-examples/c/usage-examples/bulkWrite.c b/code-examples/c/usage-examples/bulkWrite.c new file mode 100644 index 0000000..4f3ab8c --- /dev/null +++ b/code-examples/c/usage-examples/bulkWrite.c @@ -0,0 +1,41 @@ +#include +#include + +int +main (int argc, char *argv[]) +{ + mongoc_client_t *client; + mongoc_collection_t *collection; + bson_error_t error; + bson_t *document; + bool retval; + + mongoc_init (); + + client = mongoc_client_new (""); + collection = mongoc_client_get_collection (client, "sample_mflix", "theaters"); + + document = bson_new (); + BSON_APPEND_DOCUMENT_BEGIN (document, "$set", &child1); + BSON_APPEND_BOOL (&child1, "is_in_ohio", true); + bson_append_document_end (document, &child1); + + bson_t query = BSON_INITIALIZER; + BSON_APPEND_UTF8 (&query, "location.address.zipcode", "44011"); + + retval = mongoc_collection_update_many ( + collection, &query, document, NULL, NULL, &error); + + if (!retval) { + fprintf (stderr, "%s\n", error.message); + } + + bson_destroy (document); + bson_destroy (&query); + mongoc_collection_destroy (collection); + mongoc_client_destroy (client); + + mongoc_cleanup (); + + return 0; +} diff --git a/code-examples/c/usage-examples/changeStream.c b/code-examples/c/usage-examples/changeStream.c new file mode 100644 index 0000000..191b268 --- /dev/null +++ b/code-examples/c/usage-examples/changeStream.c @@ -0,0 +1,43 @@ +#include +#include + +// Replace the uri string with your MongoDB deployment's connection string. +const char *uri_string = ""; + +mongoc_client_t *client; +mongoc_collection_t *collection; +char *str; +bson_error_t error; +mongoc_change_stream_t *stream; +const bson_t *doc; + +mongoc_init (); + +client = mongoc_client_new (uri_string); +collection = mongoc_client_get_collection (client, "insertDB", "haikus"); +stream = mongoc_collection_watch ( + collection, tmp_bson ("{}"), NULL); // Create a pipeline with an empty document to watch for all changes + +if (mongoc_change_stream_error_document (stream, &error, &doc)) { + fprintf (stderr, "Error: %s\n", error.message); + return EXIT_FAILURE; +} + +while (mongoc_change_stream_next (stream, &doc)) { + str = bson_as_json (doc, NULL); + printf ("%s\n", str); + bson_free (str); +} + +if (mongoc_change_stream_error_document (stream, &error, &doc)) { + str = bson_as_json (doc, NULL); + fprintf (stderr, "Error: %s, Document: %s\n", error.message, str); + bson_free (str); +} + +mongoc_change_stream_destroy (stream); +mongoc_collection_destroy (collection); +mongoc_client_destroy (client); +mongoc_cleanup (); + +return EXIT_SUCCESS; diff --git a/code-examples/c/usage-examples/changeStream_listener.c b/code-examples/c/usage-examples/changeStream_listener.c new file mode 100644 index 0000000..1c9ad86 --- /dev/null +++ b/code-examples/c/usage-examples/changeStream_listener.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +// Replace the uri string with your MongoDB deployment's connection string +const char *uri_str = ""; + +static void +watch_change_db (mongoc_database_t * database) +{ + mongoc_change_stream_t *stream; + bson_error_t error; + const bson_t *doc; + + const char * haikus = "haikus"; + + stream = mongoc_database_watch (database,(bson_t *) haikus,NULL); + if (stream) { + printf ("received a change to the collection: \t\n"); + + while (mongoc_change_stream_next (stream, &doc)) { + char * str = bson_as_json (doc, NULL); + printf ("%s\n", str); + bson_free (str); + } + + sleep (1); + + bson_t * insert = BCON_NEW ( + "title", "Record of a Shriveled Datum", + "content", "No bytes, no problem. Just insert a document, in MongoDB" + ); + + bool r = mongoc_database_insert_one (database, haikus, insert, NULL, &error); + bson_destroy (insert); + + if (!r) { + fprintf (stderr, "Error inserting: %s\n", error.message); + } + + sleep (1); + + if (mongoc_change_stream_error_document (stream, &error, NULL)) { + fprintf (stderr, "Error reading change stream: %s\n", error.message); + return; + } + + mongoc_change_stream_close (stream); + printf ("closed the change stream\n"); + mongoc_change_stream_destroy (stream); + + mongoc_uri_destroy (uri); + mongoc_database_destroy (database); + mongoc_client_destroy (client); + + return 0; + } +} + +int +main (int argc, char *argv[]) +{ + mongoc_client_t *client = mongoc_client_new (uri_str); + + if (!client) { + fprintf (stderr, "Failed to parse URI.\n"); + return EXIT_FAILURE; + } + + mongoc_database_t * database = mongoc_client_get_database (client, "insertDB"); + watch_change_db (database); +} diff --git a/code-examples/c/usage-examples/command.c b/code-examples/c/usage-examples/command.c new file mode 100644 index 0000000..add2529 --- /dev/null +++ b/code-examples/c/usage-examples/command.c @@ -0,0 +1,36 @@ +#include +#include +#include + +int main(int argc, char *argv[]) { + mongoc_client_t *client; + mongoc_database_t *database; + bson_error_t error; + bson_t *command, reply; + bson_iter_t iter; + bson_t *doc; + char *str; + + mongoc_init (); + + client = mongoc_client_new (""); + database = mongoc_client_get_database (client, "sample_mflix"); + + command = BCON_NEW ("dbStats", BCON_INT32 (1)); + if (mongoc_database_command_simple (database, command, NULL, &reply, &error)) { + str = bson_as_canonical_extended_json (&reply, NULL); + printf ("%s\n", str); + bson_free (str); + } else { + fprintf (stderr, "%s\n", error.message); + } + + bson_destroy (command); + bson_destroy (&reply); + mongoc_database_destroy (database); + mongoc_client_destroy (client); + + mongoc_cleanup (); + + return 0; +} diff --git a/code-examples/c/usage-examples/count.c b/code-examples/c/usage-examples/count.c new file mode 100644 index 0000000..66a9e50 --- /dev/null +++ b/code-examples/c/usage-examples/count.c @@ -0,0 +1,50 @@ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + mongoc_client_t *client; + mongoc_collection_t *collection; + bson_error_t error; + bson_t *command, reply, *query; + bson_iter_t iter; + char *str; + int64_t count; + + mongoc_init(); + + client = mongoc_client_new(""); + + collection = mongoc_client_get_collection(client, "sample_mflix", "movies"); + + command = BCON_NEW("count", BCON_UTF8("movies")); + + if (mongoc_collection_command_simple(collection, command, NULL, &reply, &error)) { + if (bson_iter_init(&iter, &reply) && bson_iter_find(&iter, "n")) { + printf ("Estimated number of documents in the movies collection: %lld\n", bson_iter_as_int64 (&iter)); + } + } else { + fprintf (stderr, "%s\n", error.message); + } + + query = bson_new(); + BSON_APPEND_UTF8(query, "countries", "Canada"); + count = mongoc_collection_count_documents(collection, query, NULL, NULL, NULL, &error); + if (count != -1) { + printf ("Number of movies from Canada: %lld\n", count); + } else { + fprintf (stderr, "%s\n", error.message); + } + + bson_destroy(query); + bson_destroy(command); + bson_destroy(&reply); + + mongoc_collection_destroy(collection); + mongoc_client_destroy(client); + + mongoc_cleanup(); + + return 0; +} diff --git a/code-examples/c/usage-examples/deleteMany.c b/code-examples/c/usage-examples/deleteMany.c new file mode 100644 index 0000000..0f80f26 --- /dev/null +++ b/code-examples/c/usage-examples/deleteMany.c @@ -0,0 +1,43 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + mongoc_client_t *client; + mongoc_collection_t *collection; + bson_error_t error; + bson_t *command, reply; + bson_t *doc = NULL; + bson_t *regex = NULL; + mongoc_cursor_t *cursor; + int64_t count; + + mongoc_init(); + + client = mongoc_client_new(""); + collection = mongoc_client_get_collection(client, "sample_mflix", "movies"); + + doc = bson_new(); + regex = BCON_NEW("$regex", "Santa"); + BSON_APPEND_DOCUMENT(doc, "title", regex); + + if (!mongoc_collection_delete_many(collection, doc, NULL, &reply, &error)) { + fprintf(stderr, "%s\n", error.message); + return EXIT_FAILURE; + } + + count = bson_count_keys(&reply); + printf("Deleted %" PRId64 " documents.\n", count); + + bson_destroy(doc); + bson_destroy(regex); + bson_destroy(&reply); + + mongoc_cursor_destroy(cursor); + mongoc_collection_destroy(collection); + mongoc_client_destroy(client); + + mongoc_cleanup(); + + return EXIT_SUCCESS; +} diff --git a/code-examples/c/usage-examples/deleteOne.c b/code-examples/c/usage-examples/deleteOne.c new file mode 100644 index 0000000..e4c5d1a --- /dev/null +++ b/code-examples/c/usage-examples/deleteOne.c @@ -0,0 +1,40 @@ +#include +#include +#include + +int main (int argc, char *argv[]) +{ + mongoc_client_t *client; + mongoc_collection_t *collection; + bson_error_t error; + bson_t *query; + int64_t delete_count; + char *uri = ""; + + mongoc_init (); + + client = mongoc_client_new (uri); + collection = mongoc_client_get_collection (client, "sample_mflix", "movies"); + + query = BCON_NEW ("title", BCON_UTF8 ("Annie Hall")); + + if (mongoc_collection_delete_one (collection, query, NULL, NULL, &error)) { + delete_count = mongoc_collection_count_documents (collection, query, NULL, 0, NULL, &error); + + if (delete_count == 1) { + printf ("Successfully deleted one document.\n"); + } else { + printf ("No documents matched the query. Deleted 0 documents.\n"); + } + } else { + fprintf (stderr, "%s\n", error.message); + } + + bson_destroy (query); + mongoc_collection_destroy (collection); + mongoc_client_destroy (client); + + mongoc_cleanup (); + + return 0; +} diff --git a/code-examples/c/usage-examples/distinct.c b/code-examples/c/usage-examples/distinct.c new file mode 100644 index 0000000..05c6d18 --- /dev/null +++ b/code-examples/c/usage-examples/distinct.c @@ -0,0 +1,55 @@ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + mongoc_client_t *client; + mongoc_collection_t *collection; + mongoc_cursor_t *cursor; + const bson_t *doc; + bson_t query; + bson_t options; + bson_error_t error; + char *str; + + mongoc_init(); + + client = mongoc_client_new(""); + + if (!client) { + fprintf(stderr, "Failed to parse URI.\n"); + return EXIT_FAILURE; + } + + collection = mongoc_client_get_collection(client, "sample_mflix", "movies"); + + bson_init(&query); + bson_append_utf8(&query, "directors", -1, "Barbra Streisand", -1); + + /* Pass an empty document as the options argument to mongoc_cursor_new. */ + bson_init(&options); + + cursor = mongoc_collection_find_with_opts(collection, &query, &options, NULL); + + while (mongoc_cursor_next(cursor, &doc)) { + str = bson_as_canonical_extended_json(doc, NULL); + printf("%s\n", str); + bson_free(str); + } + + if (mongoc_cursor_error(cursor, &error)) { + fprintf(stderr, "Cursor Failure: %s\n", error.message); + return EXIT_FAILURE; + } + + bson_destroy(&query); + bson_destroy(&options); + mongoc_cursor_destroy(cursor); + mongoc_collection_destroy(collection); + mongoc_client_destroy(client); + mongoc_cleanup(); + + return EXIT_SUCCESS; + +} diff --git a/code-examples/c/usage-examples/dot-notation.c b/code-examples/c/usage-examples/dot-notation.c new file mode 100644 index 0000000..b00b4ba --- /dev/null +++ b/code-examples/c/usage-examples/dot-notation.c @@ -0,0 +1,43 @@ +#include +#include + +// start-no-error +mongoc_collection_t* collection; +mongoc_client_t* client; +bson_error_t error; +bson_t* doc; + +client = mongoc_client_new (""); +collection = mongoc_client_get_collection (client, "", ""); + +doc = BCON_NEW ("$set", "{", "field.nested", BCON_UTF8 ("A string"), "}"); + +if (!mongoc_collection_update_one (collection, NULL, doc, NULL, NULL, &error)) { + printf("%s\n", error.message); +} +// end-no-error + +// start-error +doc = BCON_NEW ("field", "{", "nested", BCON_UTF8 ("A string"), "}") + +if (!mongoc_collection_update_one (collection, NULL, doc, NULL, NULL, &error)) { + printf("%s\n", error.message); +} + +mongoc_collection_destroy(collection); +mongoc_client_destroy(client); +// end-error + +// start-no-key +client = mongoc_client_new (""); +collection = mongoc_client_get_collection (client, "", ""); + +bson_t* query = bson_new (); +BSON_APPEND_UTF8 (query, "age", "Accepts any type!"); + +mongoc_cursor_t* cursor = mongoc_collection_find_with_opts (collection, query, NULL, NULL); + +bson_destroy(query); +mongoc_collection_destroy(collection); +mongoc_client_destroy(client); +// end-no-key diff --git a/code-examples/c/usage-examples/find.c b/code-examples/c/usage-examples/find.c new file mode 100644 index 0000000..2f67587 --- /dev/null +++ b/code-examples/c/usage-examples/find.c @@ -0,0 +1,45 @@ +#include +#include +#include + +int +main (int argc, char *argv[]) +{ + mongoc_client_t *client; + mongoc_collection_t *collection; + mongoc_cursor_t *cursor; + const bson_t *doc; + bson_t *query; + char *str; + + mongoc_init (); + + client = mongoc_client_new (""); + collection = mongoc_client_get_collection (client, "sample_mflix", "movies"); + + query = bson_new (); + + BSON_APPEND_DOCUMENT_BEGIN (query, "runtime", child); + BSON_APPEND_INT32 (child, "$lt", 15); + + cursor = mongoc_collection_find_with_opts (collection, query, NULL, NULL); + + while (mongoc_cursor_next (cursor, &doc)) { + str = bson_as_json (doc, NULL); + printf ("%s\n", str); + bson_free (str); + } + + if (mongoc_cursor_error (cursor, &error)) { + fprintf (stderr, "%s\n", error.message); + } + + bson_destroy (query); + mongoc_cursor_destroy (cursor); + mongoc_collection_destroy (collection); + mongoc_client_destroy (client); + + mongoc_cleanup (); + + return 0; +} diff --git a/code-examples/c/usage-examples/findOne.c b/code-examples/c/usage-examples/findOne.c new file mode 100644 index 0000000..70bd9ce --- /dev/null +++ b/code-examples/c/usage-examples/findOne.c @@ -0,0 +1,85 @@ +#include +#include +#include + +// Struct for IMDB data +typedef struct { + int rating; + int votes; + int id; +} IMDB; + +// Struct for Movie data +typedef struct { + char *title; + int year; + date_t released; // Assuming `date_t` is a custom date type. + char *plot; + char *type; + IMDB imdb; +} Movie; + +// Function to run query +void run() { + // If adding error handling. + // bson_error_t error; + + // Replace the uri with your MongoDB deployment's connection string. + const char *uri_str = ""; + + mongoc_client_t *client; + mongoc_collection_t *collection; + bson_t* query; + mongoc_cursor_t* cursor; + const bson_t* doc; + + // Initialize the MongoDB client library. + mongoc_init(); + + client = mongoc_client_new(uri_str); + collection = mongoc_client_get_collection(client, "sample_mflix", "movies"); + + query = BCON_NEW("title", BCON_UTF8("The Room")); + + // Specify the sorting options + bson_t sort = BSON_INITIALIZER; + BSON_APPEND_INT32(&sort, "imdb.rating", -1); + + // Projection + bson_t fields = BSON_INITIALIZER; + BSON_APPEND_INT32(&fields, "_id", 0); + BSON_APPEND_INT32(&fields, "title", 1); + BSON_APPEND_INT32(&fields, "imdb", 1); + + // Execute the query. + cursor = mongoc_collection_find_with_opts(collection, query, &fields, &sort); + + // Iterate over the results and print them out. + while (mongoc_cursor_next(cursor, &doc)) { + char* str = bson_as_json(doc, NULL); + printf("%s\n", str); + bson_free(str); + } + + // Clean up + bson_destroy(query); + mongoc_cursor_destroy(cursor); + mongoc_collection_destroy(collection); + mongoc_client_destroy(client); + + /* If added error handling. + if (mongoc_cursor_error (cursor, &error)) { + fprintf (stderr, "Cursor Failure: %s\n", error.message); + exit (EXIT_FAILURE); + } + */ + + // Release our resources. + mongoc_cleanup(); + +} + +int main() { + run(); + return 0; +} diff --git a/code-examples/c/usage-examples/insertMany.c b/code-examples/c/usage-examples/insertMany.c new file mode 100644 index 0000000..ccf9321 --- /dev/null +++ b/code-examples/c/usage-examples/insertMany.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +int +main (int argc, char *argv[]) +{ + mongoc_client_t *client; + mongoc_collection_t *collection; + bson_error_t error; + bson_oid_t oid; + bson_t *doc; + char *str; + bool retval; + + mongoc_init (); + + client = mongoc_client_new (""); + collection = mongoc_client_get_collection (client, "insertDB", "foods"); + + bson_t *foods[] = { + BCON_NEW ("name", "cake", "healthy", BCON_BOOL(false)), + BCON_NEW ("name", "lettuce", "healthy", BCON_BOOL(true)), + BCON_NEW ("name", "donut", "healthy", BCON_BOOL(false)), + }; + + size_t n = sizeof(foods) / sizeof(foods[0]); + size_t insertedCount = 0; + for (size_t i=0; i +#include +#include + +int main(int argc, char *argv[]) +{ + mongoc_client_t *client; + mongoc_collection_t *collection; + bson_error_t error; + bson_oid_t oid; + bson_t *doc; + + mongoc_init (); + + // Replace with your connection string. + client = mongoc_client_new (""); + collection = mongoc_client_get_collection (client, "insertDB", "haiku"); + + doc = bson_new (); + bson_oid_init (&oid, NULL); + BSON_APPEND_OID (doc, "_id", &oid); + + BSON_APPEND_UTF8 (doc, "title", "Record of a Shriveled Datum"); + BSON_APPEND_UTF8 (doc, "content", "No bytes, no problem. Just insert a document, in MongoDB"); + + if (!mongoc_collection_insert_one (collection, doc, NULL, NULL, &error)) { + fprintf (stderr, "%s\n", error.message); + } else { + char *str = bson_oid_to_string(&oid); + printf ("A document was inserted with the _id: %s\n", str); + bson_free(str); + } + + bson_destroy (doc); + mongoc_collection_destroy (collection); + mongoc_client_destroy (client); + + mongoc_cleanup (); + + return 0; +} diff --git a/code-examples/c/usage-examples/replaceOne.c b/code-examples/c/usage-examples/replaceOne.c new file mode 100644 index 0000000..60baaab --- /dev/null +++ b/code-examples/c/usage-examples/replaceOne.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include +#include + +int main (int argc, char *argv[]) { + mongoc_client_t *client; + mongoc_collection_t *collection; + mongoc_write_concern_t *write_concern; + bson_error_t error; + bson_t *filter, *update; + bson_t reply; + bson_val_t *value; + char *str; + bool retval; + int num; + + mongoc_init (); + + client = mongoc_client_new (""); + collection = mongoc_client_get_collection (client, "sample_mflix", "movies"); + write_concern = mongoc_write_concern_new (); + mongoc_write_concern_set_w (write_concern, MONGOC_WRITE_CONCERN_W_MAJORITY); + + char title_regex[100] = "The Cat from"; + filter = BCON_NEW ("title", "{", "$regex", BCON_UTF8 (title_regex), "}"); + + srand(time(NULL)); + num = rand() % 1000 + 1; + char new_title[100]; + sprintf(new_title, "The Cat from Sector %d", num); + update = BCON_NEW ("$set", "{", "title", BCON_UTF8 (new_title), "}"); + + retval = mongoc_collection_replace_one ( + collection, + filter, + update, + write_concern, + &reply, + &error); + + if (!retval) { + fprintf (stderr, "Error: %s\n", error.message); + } else { + value = bson_get_field_value (&reply, "nModified"); + printf ("Modified %d document(s)\n", bson_value_get_int32(value)); + } + + bson_free (str); + bson_destroy (&reply); + bson_destroy (update); + bson_destroy (filter); + mongoc_write_concern_destroy (write_concern); + mongoc_collection_destroy (collection); + mongoc_client_destroy (client); + + mongoc_cleanup (); + + return 0; +} diff --git a/code-examples/c/usage-examples/updateMany.c b/code-examples/c/usage-examples/updateMany.c new file mode 100644 index 0000000..5dd7785 --- /dev/null +++ b/code-examples/c/usage-examples/updateMany.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include + +int main(int argc, char* argv[]) { + mongoc_client_t* client; + mongoc_collection_t* collection; + mongoc_update_flags_t flags; + bson_oid_t oid; + bson_error_t error; + bson_t* update; + bson_t* filter; + char* str; + long rand_percent; + long int ti; + bson_t query, set; + bool success; + + mongoc_init(); + + client = mongoc_client_new(""); + + srand((unsigned int)time(&ti)); + + collection = mongoc_client_get_collection(client, "sample_mflix", "movies"); + + filter = BCON_NEW("rated", "G"); + + rand_percent = rand() % 101; + + char random_review[60]; + sprintf(random_review, "After viewing I am %li%% more satisfied with life.", rand_percent); + + BSON_APPEND_UTF8(&set, "random_review", random_review); + + update = BCON_NEW("$set", "{", "random_review", BCON_UTF8(random_review), "}"); + + if (!(success = mongoc_collection_update_many(collection, filter, update, NULL, NULL, &error))) { + printf("Updated %d documents\n", success); + return EXIT_FAILURE; + } + + printf("Update Result: %s\n", success ? "SUCCESS" : "FAILURE"); + + bson_destroy(filter); + bson_destroy(update); + mongoc_collection_destroy(collection); + mongoc_client_destroy(client); + mongoc_cleanup(); + + return EXIT_SUCCESS; +} diff --git a/code-examples/c/usage-examples/updateOne.c b/code-examples/c/usage-examples/updateOne.c new file mode 100644 index 0000000..1032dd4 --- /dev/null +++ b/code-examples/c/usage-examples/updateOne.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + mongoc_client_t *client; + mongoc_collection_t *collection; + mongoc_write_concern_t *write_concern; + bson_error_t error; + bson_t *update = NULL; + bson_t *query = NULL; + char *str; + int exitcode = EXIT_FAILURE; + + mongoc_init(); + + client = mongoc_client_new (""); + collection = mongoc_client_get_collection (client, "sample_mflix", "movies"); + write_concern = mongoc_write_concern_new (); + + mongoc_write_concern_set_wmajority (write_concern, 10000 /* 10 seconds */); + mongoc_collection_set_write_concern (collection, write_concern); + + query = BCON_NEW ("title", BCON_UTF8 ("Random Harvest")); + update = BCON_NEW ("$set", "{", + "plot", BCON_UTF8 ("A harvest of random numbers, such as: X"), + "}"); + + // setting upsert as true + mongoc_update_flags_t upsert_flag = MONGOC_UPDATE_UPSERT; + if (mongoc_collection_update (collection, upsert_flag, query, update, write_concern, &error)) { + exitcode = EXIT_SUCCESS; + } + + // Log the number of matching and modified documents + printf("Modified %d entries\n", mongoc_collection_count(collection, MONGOC_QUERY_NONE, NULL, 0, 0, NULL, NULL)); + + if (query) + bson_destroy (query); + + if (update) + bson_destroy (update); + + mongoc_write_concern_destroy (write_concern); + mongoc_collection_destroy (collection); + mongoc_client_destroy (client); + + mongoc_cleanup (); + + return exitcode; +}