Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions code-examples/c/usage-examples/bulkWrite.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <mongoc.h>
#include <bson.h>

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 ("<connection string uri>");
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;
}
43 changes: 43 additions & 0 deletions code-examples/c/usage-examples/changeStream.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <mongoc/mongoc.h>
#include <bson/bson.h>

// Replace the uri string with your MongoDB deployment's connection string.
const char *uri_string = "<connection string uri>";

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;
73 changes: 73 additions & 0 deletions code-examples/c/usage-examples/changeStream_listener.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <bson.h>
#include <mongoc.h>
#include <stdio.h>
#include <unistd.h>

// Replace the uri string with your MongoDB deployment's connection string
const char *uri_str = "<connection string uri>";

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);
}
36 changes: 36 additions & 0 deletions code-examples/c/usage-examples/command.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <mongoc.h>
#include <stdio.h>
#include <bson.h>

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 ("<connection string uri>");
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;
}
50 changes: 50 additions & 0 deletions code-examples/c/usage-examples/count.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <bson/bson.h>
#include <mongoc/mongoc.h>
#include <stdio.h>

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("<connection string uri>");

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;
}
43 changes: 43 additions & 0 deletions code-examples/c/usage-examples/deleteMany.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <mongoc.h>
#include <stdio.h>

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("<connection string uri>");
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;
}
40 changes: 40 additions & 0 deletions code-examples/c/usage-examples/deleteOne.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <bson/bson.h>
#include <mongoc/mongoc.h>
#include <stdio.h>

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 = "<connection string 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;
}
55 changes: 55 additions & 0 deletions code-examples/c/usage-examples/distinct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <bson/bson.h>
#include <mongoc/mongoc.h>
#include <stdio.h>

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("<connection string uri>");

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;

}
Loading