mongoc_client_t — MongoDB C Driver 1.9.2

- MongoDB C Driver 1.9.2 »
- API Reference

# mongoc_client_t

A single-threaded MongoDB connection. See Connection Pooling.

## Synopsis

    typedef struct _mongoc_client_t mongoc_client_t;

    typedef mongoc_stream_t *(*mongoc_stream_initiator_t) (
       const mongoc_uri_t *uri,
       const mongoc_host_list_t *host,
       void *user_data,
       bson_error_t *error);

`mongoc_client_t` is an opaque type that provides access to a MongoDB server, replica set, or sharded cluster. It maintains management of underlying sockets and routing to individual nodes based on mongoc_read_prefs_t or mongoc_write_concern_t.

## Streams

The underlying transport for a given client can be customized, wrapped or replaced by any implementation that fulfills mongoc_stream_t. A custom transport can be set with mongoc_client_set_stream_initiator().

## Thread Safety

`mongoc_client_t` is NOT thread-safe and should only be used from one thread at a time. When used in multi-threaded scenarios, it is recommended that you use the thread-safe mongoc_client_pool_t to retrieve a `mongoc_client_t` for your thread.

## Example

example-client.c

    /* gcc example-client.c -o example-client $(pkg-config --cflags --libs
     * libmongoc-1.0) */

    /* ./example-client [CONNECTION_STRING [COLLECTION_NAME]] */

    #include <mongoc.h>
    #include <stdio.h>
    #include <stdlib.h>

    int
    main (int argc, char *argv[])
    {
       mongoc_client_t *client;
       mongoc_collection_t *collection;
       mongoc_cursor_t *cursor;
       bson_error_t error;
       const bson_t *doc;
       const char *uristr = "mongodb://127.0.0.1/?appname=client-example";
       const char *collection_name = "test";
       bson_t query;
       char *str;

       mongoc_init ();

       if (argc > 1) {
          uristr = argv[1];
       }

       if (argc > 2) {
          collection_name = argv[2];
       }

       client = mongoc_client_new (uristr);

       if (!client) {
          fprintf (stderr, "Failed to parse URI.\n");
          return EXIT_FAILURE;
       }

       mongoc_client_set_error_api (client, 2);

       bson_init (&query);

    #if 0
       bson_append_utf8 (&query, "hello", -1, "world", -1);
    #endif

       collection = mongoc_client_get_collection (client, "test", collection_name);
       cursor = mongoc_collection_find_with_opts (
          collection,
          &query,
          NULL,  /* additional options */
          NULL); /* read prefs, NULL for default */

       while (mongoc_cursor_next (cursor, &doc)) {
          str = bson_as_canonical_extended_json (doc, NULL);
          fprintf (stdout, "%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);
       mongoc_cursor_destroy (cursor);
       mongoc_collection_destroy (collection);
       mongoc_client_destroy (client);

       mongoc_cleanup ();

       return EXIT_SUCCESS;
    }

## Functions

- mongoc_client_command()
- mongoc_client_command_simple()
- mongoc_client_command_simple_with_server_id()
- mongoc_client_command_with_opts()
- mongoc_client_destroy()
- mongoc_client_find_databases_with_opts()
- mongoc_client_get_collection()
- mongoc_client_get_database()
- mongoc_client_get_database_names()
- mongoc_client_get_database_names_with_opts()
- mongoc_client_get_default_database()
- mongoc_client_get_gridfs()
- mongoc_client_get_max_bson_size()
- mongoc_client_get_max_message_size()
- mongoc_client_get_read_concern()
- mongoc_client_get_read_prefs()
- mongoc_client_get_server_description()
- mongoc_client_get_server_descriptions()
- mongoc_client_get_server_status()
- mongoc_client_get_uri()
- mongoc_client_get_write_concern()
- mongoc_client_new()
- mongoc_client_new_from_uri()
- mongoc_client_read_command_with_opts()
- mongoc_client_read_write_command_with_opts()
- mongoc_client_select_server()
- mongoc_client_set_apm_callbacks()
- mongoc_client_set_appname()
- mongoc_client_set_error_api()
- mongoc_client_set_read_concern()
- mongoc_client_set_read_prefs()
- mongoc_client_set_ssl_opts()
- mongoc_client_set_stream_initiator()
- mongoc_client_set_write_concern()
- mongoc_client_start_session()
- mongoc_client_write_command_with_opts()

Created using Sphinx 1.6.5, based on Sphinx Readable Theme.