libmongo-client  0.1.8
Creating collections

In this simple example we'll learn how to explicitly create collections with the library, be those normal collections, capped ones or simply preallocated.

Our application will attempt to create a normal collection, a capped one, a capped one that's also capped on the number of entries, and a pre-allocated (but uncapped) collection.

It will print these properties of the collections aswell, so that we can verify that the creation did indeed work.

#include <mongo.h>
#include <errno.h>
#include <stdio.h>

First of all, we need a function that prints the collection properties. Because we're lazy, it will take a BSON object, as returned by mongo_sync_cmd_exists().

The output of said command is a BSON object that has a name field, which is the full name of the collection, the database part included; and an options subdocument, which lists various options specified during creating, such as cappedness, size and maximum number of elements.

Our very simple function will extract all these and print what's appropriate. It will also free the BSON object it was given, so that we don't leak memory.

static void
print_coll_info (bson *info)
{
bson_cursor *c = NULL;
bson *options = NULL;
const gchar *name;
gboolean capped = FALSE;
gint64 size = -1;
gint64 max = -1;
c = bson_find (info, "name");
bson_cursor_find (c, "options");
bson_cursor_get_document (c, &options);
printf ("Options for %s:\n", name);
bson_free (info);
c = bson_find (options, "capped");
c = bson_find (options, "size");
c = bson_find (options, "max");
bson_free (options);
printf ("\tCapped: %s\n", (capped) ? "yes" : "no");
if (size > 0)
printf ("\tSize : %lu\n", size);
if (max > 0)
printf ("\tMax : %lu\n", max);
printf ("\n");
}

With that done, lets get down to business, and create the collections, after connecting to the server, of course.

int
main (void)
{
mongo_sync_connection *conn;
conn = mongo_sync_connect ("localhost", 27017, FALSE);
if (!conn)
{
fprintf (stderr, "Connection failed: %s\n", strerror (errno));
return 1;
}

First we create a completely normal collection, with the default settings:

mongo_sync_cmd_create (conn, "lmc", "cmd_create", MONGO_COLLECTION_DEFAULTS);
print_coll_info (mongo_sync_cmd_exists (conn, "lmc", "cmd_create"));

Then a capped collection:

mongo_sync_cmd_create (conn, "lmc", "cmd_create_capped",
print_coll_info (mongo_sync_cmd_exists (conn, "lmc", "cmd_create_capped"));

Followed by another capped collection, one that is also capped by the number of elements, not only by size:

mongo_sync_cmd_create (conn, "lmc", "cmd_create_capped_max",
655360, 100);
print_coll_info (mongo_sync_cmd_exists (conn, "lmc",

And finally, we create a pre-allocated collection:

"cmd_create_capped_max"));
mongo_sync_cmd_create (conn, "lmc", "cmd_create_sized",
print_coll_info (mongo_sync_cmd_exists (conn, "lmc", "cmd_create_sized"));

And that's about it, really.

return 0;
}
bson_cursor_free
void bson_cursor_free(bson_cursor *c)
Delete a cursor, and free up all resources used by it.
Definition: bson.c:800
MONGO_COLLECTION_DEFAULTS
@ MONGO_COLLECTION_DEFAULTS
Default options.
Definition: mongo-sync.h:404
mongo_sync_connect
mongo_sync_connection * mongo_sync_connect(const gchar *address, gint port, gboolean slaveok)
Synchronously connect to a MongoDB server.
Definition: mongo-sync.c:201
mongo_sync_cmd_exists
bson * mongo_sync_cmd_exists(mongo_sync_connection *conn, const gchar *db, const gchar *coll)
Check whether a collection exists in MongoDB.
Definition: mongo-sync.c:1255
MONGO_COLLECTION_CAPPED
@ MONGO_COLLECTION_CAPPED
The collection is capped.
Definition: mongo-sync.h:406
bson_cursor_get_int64
gboolean bson_cursor_get_int64(const bson_cursor *c, gint64 *dest)
Get the value stored at the cursor, as a 64-bit integer.
Definition: bson.c:1240
bson_cursor_get_string
gboolean bson_cursor_get_string(const bson_cursor *c, const gchar **dest)
Get the value stored at the cursor, as string.
Definition: bson.c:1007
mongo_sync_cmd_create
gboolean mongo_sync_cmd_create(mongo_sync_connection *conn, const gchar *db, const gchar *coll, gint flags,...)
Create a new MongoDB collection.
Definition: mongo-sync.c:1181
bson_free
void bson_free(bson *b)
Free the memory associated with a BSON object.
Definition: bson.c:579
MONGO_COLLECTION_CAPPED_MAX
@ MONGO_COLLECTION_CAPPED_MAX
The collection is capped by element number aswell.
Definition: mongo-sync.h:408
bson_cursor_find
gboolean bson_cursor_find(bson_cursor *c, const gchar *name)
Move the cursor to a given key.
Definition: bson.c:935
bson_find
bson_cursor * bson_find(const bson *b, const gchar *name)
Create a new cursor positioned at a given key.
Definition: bson.c:955
bson_cursor_get_boolean
gboolean bson_cursor_get_boolean(const bson_cursor *c, gboolean *dest)
Get the value stored at the cursor, as a boolean.
Definition: bson.c:1113
MONGO_COLLECTION_SIZED
@ MONGO_COLLECTION_SIZED
The collection needs to be pre-allocated.
Definition: mongo-sync.h:412
mongo_sync_disconnect
void mongo_sync_disconnect(mongo_sync_connection *conn)
Close and free a synchronous MongoDB connection.
Definition: mongo-sync.c:396
bson_cursor_get_document
gboolean bson_cursor_get_document(const bson_cursor *c, bson **dest)
Get the value stored at the cursor, as a BSON document.
Definition: bson.c:1034