#include <mongoc/mongoc.h>
int main () {
mongoc_client_t *client;
mongoc_database_t *database;
mongoc_collection_t *collection;
mongoc_cursor_t *cursor;
const bson_t *doc;
bson_t query;
bson_t sort;
mongoc_init ();
client = mongoc_client_new ("mongodb://localhost:27017");
database = mongoc_client_get_database (client, "your_database_name");
collection = mongoc_client_get_collection (client, "your_database_name", "your_collection_name");
bson_init (&query);
cursor = mongoc_collection_find_with_opts (collection, &query, NULL, NULL);
bson_init (&sort);
BSON_APPEND_INT32 (&sort, "your_field_name", -1);
mongoc_cursor_sort (cursor, &sort);
if (mongoc_cursor_next (cursor, &doc)) {
/ Access the latest document here /
}
mongoc_cursor_destroy (cursor);
bson_destroy (&query);
bson_destroy (&sort);
mongoc_collection_destroy (collection);
mongoc_database_destroy (database);
mongoc_client_destroy (client);
mongoc_cleanup ();
return 0;
}