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");
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
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
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
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
bson_cursor * bson_find(const bson *b, const gchar *name)
Create a new cursor positioned at a given key.
Definition: bson.c:955
void bson_cursor_free(bson_cursor *c)
Delete a cursor, and free up all resources used by it.
Definition: bson.c:800
gboolean bson_cursor_find(bson_cursor *c, const gchar *name)
Move the cursor to a given key.
Definition: bson.c:935
void bson_free(bson *b)
Free the memory associated with a BSON object.
Definition: bson.c:579
}

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;
}
mongo_sync_connection * mongo_sync_connect(const gchar *address, gint port, gboolean slaveok)
Synchronously connect to a MongoDB server.
Definition: mongo-sync.c:201

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"));
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
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
@ MONGO_COLLECTION_DEFAULTS
Default options.
Definition: mongo-sync.h:404

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"));
@ MONGO_COLLECTION_CAPPED
The collection is capped.
Definition: mongo-sync.h:406

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",
@ MONGO_COLLECTION_CAPPED_MAX
The collection is capped by element number aswell.
Definition: mongo-sync.h:408

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"));
@ MONGO_COLLECTION_SIZED
The collection needs to be pre-allocated.
Definition: mongo-sync.h:412

And that's about it, really.

return 0;
}
void mongo_sync_disconnect(mongo_sync_connection *conn)
Close and free a synchronous MongoDB connection.
Definition: mongo-sync.c:396