Skip to content

Instantly share code, notes, and snippets.

@allanjos
Last active November 28, 2018 05:02
Show Gist options
  • Select an option

  • Save allanjos/05a25051190d2ac066d9ddcf133edecc to your computer and use it in GitHub Desktop.

Select an option

Save allanjos/05a25051190d2ac066d9ddcf133edecc to your computer and use it in GitHub Desktop.
MongoDB build sample - Fedora
$ sudo dnf install mongodb mongodb-server
$ sudo dnf install mongo-c*driver
$ sudo dnf install mongo-c*driver-devel
$ sudo dnf install mongodb*devel
$ sudo dnf install mongo*devel
$ sudo dnf install mongodb*devel
$ sudo dnf install mongo-c*driver-devel
$ sudo dnf install libmongo-client.x86_64
$ sudo dnf install mongo-c-driver.x86_64
mongo.c:
#include "mongoc.h"
int main() {
const char *uri_string = "mongodb://localhost:27017";
mongoc_uri_t *uri;
mongoc_client_t *client;
mongoc_database_t *database;
mongoc_collection_t *collection;
bson_t *command, reply, *insert;
bson_error_t error;
char *str;
bool retval;
mongoc_init();
uri = mongoc_uri_new_with_error (uri_string, &error);
if (!uri) {
fprintf(stderr, "failed to parse URI: %s\n"
"error message: %s\n",
uri_string,
error.message);
return EXIT_FAILURE;
}
client = mongoc_client_new_from_uri(uri);
if (!client) {
return EXIT_FAILURE;
}
/*
* Register the application name so we can track it in the profile logs
* on the server. This can also be done from the URI (see other examples).
*/
mongoc_client_set_appname (client, "connect-example");
/*
* Get a handle on the database "db_name" and collection "coll_name"
*/
database = mongoc_client_get_database (client, "db_name");
collection = mongoc_client_get_collection (client, "db_name", "coll_name");
/*
* Do work. This example pings the database, prints the result as JSON and
* performs an insert
*/
command = BCON_NEW ("ping", BCON_INT32 (1));
retval = mongoc_client_command_simple (
client, "admin", command, NULL, &reply, &error);
if (!retval) {
fprintf (stderr, "%s\n", error.message);
return EXIT_FAILURE;
}
str = bson_as_json (&reply, NULL);
printf ("%s\n", str);
insert = BCON_NEW ("hello", BCON_UTF8 ("world"));
if (!mongoc_collection_insert_one (collection, insert, NULL, NULL, &error)) {
fprintf (stderr, "%s\n", error.message);
}
bson_destroy (insert);
bson_destroy (&reply);
bson_destroy (command);
bson_free (str);
/*
* Release our handles and clean up libmongoc
*/
mongoc_collection_destroy (collection);
mongoc_database_destroy (database);
mongoc_uri_destroy (uri);
mongoc_client_destroy (client);
mongoc_cleanup ();
return EXIT_SUCCESS;
}
$ gcc -I /usr/include/libmongoc-1.0 -I /usr/include/libbson-1.0 mongo.c -lmongoc-1.0 -lbson-1.0
References:
- http://mongoc.org/libmongoc/current/tutorial.html
- http://mongoc.org/libmongoc/1.9.2/tutorial.html
- https://docs.mongodb.com/manual/tutorial/getting-started/
- https://www.tutorialspoint.com/mongodb/
- https://developer.fedoraproject.org/tech/database/mongodb/about.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment