Loading...
Searching...
No Matches
Creating indexes

This example will show how to properly create indexes, how to verify its success, and will also give hints on how to prepare the BSON used to create the index.

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

We will be creating and verifying multiple indexes, so lets create a function that takes a connection, a prepared index, and does the create and verify magic.

static void
create_and_verify_index(mongo_sync_connection *conn,
bson *index)
{
if (!mongo_sync_cmd_index_create (conn, "lmc.indexed", index,
MONGO_INDEX_UNIQUE | MONGO_INDEX_DROP_DUPS |
MONGO_INDEX_SPARSE))
{
gchar *error = NULL;
int e = errno;
mongo_sync_cmd_get_last_error (conn, "lmc.indexed", &error);
fprintf (stderr, "Can't create indexes: %s\n", error ? error : strerror (e));
g_free (error);
}
mongo_sync_cmd_index_create
gboolean mongo_sync_cmd_index_create(mongo_sync_connection *conn, const gchar *ns, const bson *key, gint options)
Create an index.
Definition mongo-sync.c:1900
mongo_sync_cmd_get_last_error
gboolean mongo_sync_cmd_get_last_error(mongo_sync_connection *conn, const gchar *db, gchar **error)
Get the last error from MongoDB.
Definition mongo-sync.c:1399
MONGO_INDEX_DROP_DUPS
@ MONGO_INDEX_DROP_DUPS
Drop duplicate entries when creating the indexes.
Definition mongo-sync.h:585
MONGO_INDEX_UNIQUE
@ MONGO_INDEX_UNIQUE
Create a unique index.
Definition mongo-sync.h:584
MONGO_INDEX_SPARSE
@ MONGO_INDEX_SPARSE
Create sparse indexes.
Definition mongo-sync.h:589
else
printf ("Index successfully created!\n");
}

This will create the index, and if it succeeds, write that to stdout. If it fails, it will try to query the last error, and print that to stderr.

All we have to do past this point, is to build a few index specifications in BSON, and see what happens:

int
main (void)
{
mongo_sync_connection *conn;
bson *invalid_index, *index;
invalid_index = bson_build (BSON_TYPE_STRING, "name", "", -1,
bson_build
bson * bson_build(bson_type type, const gchar *name,...)
Build a BSON object in one go.
Definition bson.c:448
BSON_TYPE_STRING
@ BSON_TYPE_STRING
4byte length + NULL terminated string
Definition bson.h:67
BSON_TYPE_NONE);
bson_finish (invalid_index);
bson_finish
gboolean bson_finish(bson *b)
Finish a BSON object.
Definition bson.c:521
BSON_TYPE_NONE
@ BSON_TYPE_NONE
Only used for errors.
Definition bson.h:65

The first index spec we create will have a single index field, name, where we set the value to an empty string. However - as we will soon see - this is not a valid specification, as MongoDB does not accept string-typed fields in the index spec.

index = bson_build (BSON_TYPE_INT32, "name", 1,
BSON_TYPE_INT32
@ BSON_TYPE_INT32
4byte integer
Definition bson.h:84
BSON_TYPE_NONE);
bson_finish (index);

Armed with the knowledge that strings are not going to work, we turn to our trusty old integers. Integers (32-bit integers at that, there really is no need to use a 64-bit value here) are the best fit for the type of an index field, because one can tell MongoDB the sort order (with negative or positive numbers) with them easily.

conn = mongo_sync_connect ("localhost", 27017, FALSE);
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
if (!conn)
{
fprintf (stderr, "Connection failed: %s\n", strerror (errno));
return 1;
}

We now have two index specs in BSON, and an established connection, lets see what happens!

create_and_verify_index (conn, invalid_index);
create_and_verify_index (conn, index);

The first will - as explained above - fail, the second will succeed.

And that is all it takes to create simple indexes! We now free up our BSON objects and disconnect, and the tutorial program is all done and finished.

bson_free (invalid_index);
bson_free
void bson_free(bson *b)
Free the memory associated with a BSON object.
Definition bson.c:579
bson_free (index);
mongo_sync_disconnect (conn);
mongo_sync_disconnect
void mongo_sync_disconnect(mongo_sync_connection *conn)
Close and free a synchronous MongoDB connection.
Definition mongo-sync.c:396
return 0;
}