Last active
July 9, 2022 06:21
-
-
Save mattlord/4926ddb4a1d46292e1296f9951f7ca17 to your computer and use it in GitHub Desktop.
Example MongoDB Embedded Application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <mongoc_embedded/mongoc_embedded.h> | |
int main (int argc, char *argv[]) | |
{ | |
mongo_embedded_v1_status *status; | |
mongo_embedded_v1_lib *lib; | |
mongo_embedded_v1_instance *instance; | |
mongoc_client_t *client; | |
mongoc_collection_t *collection; | |
mongoc_cursor_t *cursor; | |
bson_t *insert, *query, *opts, *doc; | |
bson_error_t error; | |
char *str, *name, *message; | |
/* | |
* If they've specified their name as the first arg, let's use it in the doc | |
*/ | |
if (argc > 1){ | |
if (strcmp (argv[1], "--help") == 0 || argc > 3) { | |
printf ("Usage: iot_guestbook [name] [message]\n"); | |
return EXIT_FAILURE; | |
} | |
name = argv[1]; | |
} else { | |
name = "anonymous"; | |
} | |
/* | |
* If they've specified a message as the second arg, let's use it in the doc | |
*/ | |
if (argc > 2){ | |
message = argv[2]; | |
} else { | |
message = "Hello IoT World"; | |
} | |
/* | |
* Create a new local storage instance | |
*/ | |
status = mongo_embedded_v1_status_create(); | |
mongoc_init (); | |
mongo_embedded_v1_init_params params; | |
params.yaml_config = "dbpath=/tmp"; | |
params.log_flags = 0; | |
params.log_callback = NULL; | |
params.log_user_data = NULL; | |
mongoc_log_set_handler (NULL, NULL); | |
lib = mongo_embedded_v1_lib_init (¶ms, status); | |
if (!lib) { | |
fprintf (stderr, "Could not get a handle to the Embedded library: %s!\n", mongo_embedded_v1_status_get_explanation (status)); | |
return EXIT_FAILURE; | |
} | |
instance = mongo_embedded_v1_instance_create (lib, params.yaml_config, status); | |
if (!instance) { | |
fprintf (stderr, "Could not intialize local storage instance: %s!\n", mongo_embedded_v1_status_get_explanation (status)); | |
return EXIT_FAILURE; | |
} | |
/* | |
* Create a mongoc client that can be used with the standard C driver calls | |
*/ | |
client = mongoc_embedded_v1_client_create (instance); | |
if (!client) { | |
fprintf (stderr, "Could not create mongodb client!\n"); | |
return EXIT_FAILURE; | |
} | |
/* | |
* Get a handle on the "sensor_data" collection in the "iot_test" database | |
*/ | |
collection = mongoc_client_get_collection (client, "iot_test", "sensor_data"); | |
/* | |
* We will insert a new document in the collection and then print out | |
* all existing documents in the collection | |
*/ | |
insert = bson_new (); | |
BSON_APPEND_UTF8 (insert, "message", message); | |
BSON_APPEND_UTF8 (insert, "from", name); | |
// need to multiply the unix timestamp as MongoDB Dates are milliseconds since unix epoch | |
int64_t curtime = time (NULL); | |
bson_append_date_time (insert, "date", -1, curtime*1000); | |
if (!mongoc_collection_insert_one (collection, insert, NULL, NULL, &error)) { | |
fprintf (stderr, "Could not insert document: %s\n", error.message); | |
return EXIT_FAILURE; | |
} | |
/* | |
* This is where we'll specify our query predicates and other modifiers | |
*/ | |
query = bson_new (); | |
opts = BCON_NEW ("projection", "{", | |
"_id", BCON_BOOL (false), | |
"from", BCON_BOOL (true), | |
"message", BCON_BOOL (true), | |
"date", BCON_BOOL (true), | |
"}", | |
"sort", "{", "date", BCON_INT32 (1), "}" | |
); | |
cursor = mongoc_collection_find_with_opts (collection, query, opts, NULL); | |
if (!cursor || mongoc_cursor_error (cursor, &error)) { | |
fprintf (stderr, "An error occurred: %s\n", error.message); | |
return EXIT_FAILURE; | |
} | |
while (mongoc_cursor_next (cursor, &doc)) { | |
str = bson_as_json (doc, NULL); | |
printf ("%s\n", str); | |
bson_free (str); | |
} | |
/* | |
* Release our handles and clean everything up | |
*/ | |
bson_destroy (insert); | |
bson_destroy (query); | |
bson_destroy (opts); | |
bson_destroy (doc); | |
mongoc_cursor_destroy (cursor); | |
mongoc_collection_destroy (collection); | |
mongoc_client_destroy (client); | |
mongoc_cleanup (); | |
mongo_embedded_v1_instance_destroy (instance, status); | |
mongo_embedded_v1_lib_fini (lib, status); | |
mongo_embedded_v1_status_destroy (status); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment