static void print_bson (const bson_t *b) { char *str; str = bson_as_canonical_extended_json (b, NULL); fprintf (stdout, "%s\n", str); bson_free (str); }
static void bulk_collation (mongoc_collection_t *collection) { mongoc_bulk_operation_t *bulk; bson_t *opts; bson_t *doc; bson_t *selector; bson_t *update; bson_error_t error; bson_t reply; char *str; uint32_t ret; /* insert {_id: "one"} and {_id: "One"} */ bulk = mongoc_collection_create_bulk_operation_with_opts ( collection, NULL); doc = BCON_NEW ("_id", BCON_UTF8 ("one")); mongoc_bulk_operation_insert (bulk, doc); bson_destroy (doc); doc = BCON_NEW ("_id", BCON_UTF8 ("One")); mongoc_bulk_operation_insert (bulk, doc); bson_destroy (doc); /* "One" normally sorts before "one"; make "one" come first */ opts = BCON_NEW ("collation", "{", "locale", BCON_UTF8 ("en_US"), "caseFirst", BCON_UTF8 ("lower"), "}"); /* set x=1 on the document with _id "One", which now sorts after "one" */ update = BCON_NEW ("$set", "{", "x", BCON_INT64 (1), "}"); selector = BCON_NEW ("_id", "{", "$gt", BCON_UTF8 ("one"), "}"); mongoc_bulk_operation_update_one_with_opts ( bulk, selector, update, opts, &error); ret = mongoc_bulk_operation_execute (bulk, &reply, &error); str = bson_as_canonical_extended_json (&reply, NULL); printf ("%s\n", str); bson_free (str); if (!ret) { printf ("Error: %s\n", error.message); } bson_destroy (&reply); bson_destroy (update); bson_destroy (selector); bson_destroy (opts); mongoc_bulk_operation_destroy (bulk); }
static void print_pipeline (mongoc_collection_t *collection) { mongoc_cursor_t *cursor; bson_error_t error; const bson_t *doc; bson_t *pipeline; char *str; pipeline = BCON_NEW ("pipeline", "[", "{", "$group", "{", "_id", "$state", "total_pop", "{", "$sum", "$pop", "}", "}", "}", "{", "$match", "{", "total_pop", "{", "$gte", BCON_INT32 (10000000), "}", "}", "}", "]"); cursor = mongoc_collection_aggregate ( collection, MONGOC_QUERY_NONE, pipeline, NULL, 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); } mongoc_cursor_destroy (cursor); bson_destroy (pipeline); }
int main (int argc, const char **argv) { bson_t *b; char *j; b = BCON_NEW ("hello", BCON_UTF8 ("bson!")); j = bson_as_canonical_extended_json (b, NULL); printf ("%s\n", j); bson_free (j); bson_destroy (b); return 0; }
int main (int argc, char *argv[]) { bson_t *bson; char *json; bson = COL_VIEW_CREATE ( SORT ("a", BCON_INT32 (1)), QUERY ("hello", "world"), LIMIT (10)); json = bson_as_canonical_extended_json (bson, NULL); printf ("%s\n", json); bson_free (json); bson_destroy (bson); return 0; }
static void bulk4 (mongoc_collection_t *collection) { mongoc_write_concern_t *wc; mongoc_bulk_operation_t *bulk; bson_error_t error; bson_t *doc; bson_t reply; char *str; bool ret; wc = mongoc_write_concern_new (); mongoc_write_concern_set_w (wc, 4); mongoc_write_concern_set_wtimeout (wc, 100); /* milliseconds */ bulk = mongoc_collection_create_bulk_operation (collection, true, wc); /* Two inserts */ doc = BCON_NEW ("_id", BCON_INT32 (10)); mongoc_bulk_operation_insert (bulk, doc); bson_destroy (doc); doc = BCON_NEW ("_id", BCON_INT32 (11)); mongoc_bulk_operation_insert (bulk, doc); bson_destroy (doc); ret = mongoc_bulk_operation_execute (bulk, &reply, &error); str = bson_as_canonical_extended_json (&reply, NULL); printf ("%s\n", str); bson_free (str); if (!ret) { printf ("Error: %s\n", error.message); } bson_destroy (&reply); mongoc_bulk_operation_destroy (bulk); mongoc_write_concern_destroy (wc); }
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 *collection_name = "test"; bson_t query; char *str; const char *uri_string = "mongodb://127.0.0.1/?appname=client-example"; mongoc_uri_t *uri; mongoc_init (); if (argc > 1) { uri_string = argv[1]; } if (argc > 2) { collection_name = argv[2]; } 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; } 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_uri_destroy (uri); mongoc_client_destroy (client); mongoc_cleanup (); return EXIT_SUCCESS; }
/* * main -- * * Connects to a server and reads BSON from it. This program takes the * following command line options: * * -h Print this help and exit. * -s SERVER_NAME Specify the host name of the server. * -p PORT_NUM Specify the port number to connect to on the server. */ int main (int argc, char *argv[]) { bson_reader_t *reader; char *hostname = NULL; char *json; char *port = NULL; const bson_t *document; int fd; int opt; opterr = 1; /* * Parse command line arguments. */ while ((opt = getopt (argc, argv, "hs:p:")) != -1) { switch (opt) { case 'h': fprintf ( stdout, "Usage: %s [-s SERVER_NAME] [-p PORT_NUM]\n", argv[0]); free (hostname); free (port); return EXIT_SUCCESS; case 'p': free (port); port = (char *) malloc (strlen (optarg) + 1); strcpy (port, optarg); break; case 's': free (hostname); hostname = (char *) malloc (strlen (optarg) + 1); strcpy (hostname, optarg); break; default: fprintf (stderr, "Unknown option: %s\n", optarg); } } /* * Open a file descriptor on the remote and read in BSON documents, one by * one. As an example of processing, this prints the incoming documents as * JSON onto STDOUT. */ fd = bson_streaming_remote_open (hostname, port); if (fd == -1) { free (hostname); free (port); return EXIT_FAILURE; } reader = bson_reader_new_from_fd (fd, true); while ((document = bson_reader_read (reader, NULL))) { json = bson_as_canonical_extended_json (document, NULL); fprintf (stdout, "%s\n", json); bson_free (json); } /* * Destroy the reader, which performs cleanup. The ``true'' argument passed * to bson_reader_new_from_fd tells libbson to close the file descriptor on * destruction. */ bson_reader_destroy (reader); free (hostname); free (port); return EXIT_SUCCESS; }
int main (int argc, char *argv[]) { const char *uri_string = "mongodb://localhost:27017/?appname=new-gridfs-example"; mongoc_client_t *client; mongoc_database_t *db; mongoc_stream_t *file_stream; mongoc_gridfs_bucket_t *bucket; mongoc_cursor_t *cursor; bson_t filter; bool res; bson_value_t file_id; bson_error_t error; const bson_t *doc; char *str; mongoc_init (); if (argc != 3) { fprintf (stderr, "usage: %s SOURCE_FILE_PATH FILE_COPY_PATH\n", argv[0]); return EXIT_FAILURE; } /* 1. Make a bucket. */ client = mongoc_client_new (uri_string); db = mongoc_client_get_database (client, "test"); bucket = mongoc_gridfs_bucket_new (db, NULL, NULL); /* 2. Insert a file. */ file_stream = mongoc_stream_file_new_for_path (argv[1], O_RDONLY, 0); res = mongoc_gridfs_bucket_upload_from_stream ( bucket, "my-file", file_stream, NULL, &file_id, &error); if (!res) { printf ("Error uploading file: %s\n", error.message); return EXIT_FAILURE; } mongoc_stream_close (file_stream); mongoc_stream_destroy (file_stream); /* 3. Download the file in GridFS to a local file. */ file_stream = mongoc_stream_file_new_for_path (argv[2], O_CREAT | O_RDWR, 0); if (!file_stream) { perror ("Error opening file stream"); return EXIT_FAILURE; } res = mongoc_gridfs_bucket_download_to_stream ( bucket, &file_id, file_stream, &error); if (!res) { printf ("Error downloading file to stream: %s\n", error.message); return EXIT_FAILURE; } mongoc_stream_close (file_stream); mongoc_stream_destroy (file_stream); /* 4. List what files are available in GridFS. */ bson_init (&filter); cursor = mongoc_gridfs_bucket_find (bucket, &filter, NULL); while (mongoc_cursor_next (cursor, &doc)) { str = bson_as_canonical_extended_json (doc, NULL); printf ("%s\n", str); bson_free (str); } /* 5. Delete the file that we added. */ res = mongoc_gridfs_bucket_delete_by_id (bucket, &file_id, &error); if (!res) { printf ("Error deleting the file: %s\n", error.message); return EXIT_FAILURE; } /* 6. Cleanup. */ mongoc_stream_close (file_stream); mongoc_stream_destroy (file_stream); mongoc_cursor_destroy (cursor); bson_destroy (&filter); mongoc_gridfs_bucket_destroy (bucket); mongoc_database_destroy (db); mongoc_client_destroy (client); mongoc_cleanup (); return EXIT_SUCCESS; }
int main (int argc, char *argv[]) { mongoc_database_t *database; mongoc_client_t *client; bson_t reply; uint16_t port; bson_error_t error; bson_t ping; char *host_and_port; char *str; bool r; if (argc < 2 || argc > 3) { fprintf (stderr, "usage: %s HOSTNAME [PORT]\n", argv[0]); return 1; } mongoc_init (); port = (argc == 3) ? atoi (argv[2]) : 27017; if (!strncmp (argv[1], "mongodb://", 10) || !strncmp (argv[1], "mongodb+srv://", 14)) { host_and_port = bson_strdup (argv[1]); } else { host_and_port = bson_strdup_printf ("mongodb://%s:%hu", argv[1], port); } client = mongoc_client_new (host_and_port); if (!client) { fprintf (stderr, "Invalid hostname or port: %s\n", host_and_port); bson_free (host_and_port); return 2; } bson_free (host_and_port); mongoc_client_set_error_api (client, 2); bson_init (&ping); bson_append_int32 (&ping, "ping", 4, 1); database = mongoc_client_get_database (client, "test"); r = mongoc_database_command_with_opts ( database, &ping, NULL, NULL, &reply, &error); if (r) { str = bson_as_canonical_extended_json (&reply, NULL); fprintf (stdout, "%s\n", str); bson_free (str); } else { fprintf (stderr, "Ping failure: %s\n", error.message); } bson_destroy (&ping); bson_destroy (&reply); mongoc_database_destroy (database); mongoc_client_destroy (client); return r ? 0 : 3; }