Loading...
Searching...
No Matches
Querying documents

We can connect, insert, and we still vaguely remember how to build BSON objects: how about we go out and query the database this time?

void
tut_sync_query_simple (void)
{

First, we define a couple of variables: a connection, a packet, a cursor, a BSON object and a counter:

mongo_sync_connection *conn;
mongo_packet *p;
mongo_sync_cursor *cursor;
bson *query;
gint i = 0;

We'll use the packet as an intermediate step between querying and making a cursor. We'll see why later. The query will be used to limit the documents queried, and the counter is just for pretty printing.

Then we do our routine connect:

conn = mongo_sync_connect ("localhost", 27017, FALSE);
if (!conn)
{
perror ("mongo_sync_connect()");
exit (1);
}
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

And then build a BSON object, an empty one, because an empty query means we're interested in all of the documents!

query = bson_new ();
bson_finish (query);
bson_new
bson * bson_new(void)
Create a new BSON object.
Definition bson.c:252
bson_finish
gboolean bson_finish(bson *b)
Finish a BSON object.
Definition bson.c:521

Now we have a connection, and we have a query, lets tell MongoDB what we want:

p = mongo_sync_cmd_query (conn, "tutorial.docs", 0,
0, 10, query, NULL);
if (!p)
{
perror ("mongo_sync_cmd_query()");
exit (1);
}
bson_free (query);
bson_free
void bson_free(bson *b)
Free the memory associated with a BSON object.
Definition bson.c:579
mongo_sync_cmd_query
mongo_packet * mongo_sync_cmd_query(mongo_sync_connection *conn, const gchar *ns, gint32 flags, gint32 skip, gint32 ret, const bson *query, const bson *sel)
Send a query command to MongoDB.
Definition mongo-sync.c:986

The first two parameters are obvious. The third is a set of flags - but we don't use any right now. Then comes the number of documents to skip, and the number of documents to return, followed by a query, and an optional field selector, which we just left empty (meaning we want all fields returned).

There's more than one way to figure out the data returned by a query: we can either use the returned packet as-is, and extract data from it using the low-level mongo_wire family of functions. Or we can make a cursor out of this packet, and iterate over the elements:

cursor = mongo_sync_cursor_new (conn, "tutorial.docs", p);
if (!cursor)
{
perror ("mongo_sync_cursor_new()");
exit (1);
}
mongo_sync_cursor_new
mongo_sync_cursor * mongo_sync_cursor_new(mongo_sync_connection *conn, const gchar *ns, mongo_packet *packet)
Create a new MongoDB Cursor.
Definition mongo-sync-cursor.c:28
while (mongo_sync_cursor_next (cursor))
{
bson *result = mongo_sync_cursor_get_data (cursor);
bson_cursor *c;
if (!result)
{
perror ("mongo_sync_cursor_get_data()");
exit (1);
}
mongo_sync_cursor_next
gboolean mongo_sync_cursor_next(mongo_sync_cursor *cursor)
Iterate a MongoDB cursor.
Definition mongo-sync-cursor.c:56
mongo_sync_cursor_get_data
bson * mongo_sync_cursor_get_data(mongo_sync_cursor *cursor)
Retrieve the BSON document at the cursor's position.
Definition mongo-sync-cursor.c:99

The first thing we do inside of the loop is to get the data from the cursor - or bail out with an error if we can't.

printf ("Keys in document #%d:\n", i);

Then we proceed to make a BSON cursor, and print all the keys that belong to the document.

Once that's done, we free the resources we used, and continue along the loop, until our cursor signals the end of the query.

c = bson_cursor_new (result);
while (bson_cursor_next (c))
printf ("\t%s\n", bson_cursor_key (c));
bson_cursor_new
bson_cursor * bson_cursor_new(const bson *b)
Create a new cursor.
Definition bson.c:786
bson_cursor_key
const gchar * bson_cursor_key(const bson_cursor *c)
Determine the name of the current elements key.
Definition bson.c:990
bson_cursor_next
gboolean bson_cursor_next(bson_cursor *c)
Position the cursor to the next key.
Definition bson.c:863
i++;
bson_cursor_free (c);
bson_free (result);
}
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

Then we clean up and go home:

mongo_sync_cursor_free (cursor);
mongo_sync_disconnect (conn);
}
mongo_sync_cursor_free
void mongo_sync_cursor_free(mongo_sync_cursor *cursor)
Free a MongoDB cursor.
Definition mongo-sync-cursor.c:83
mongo_sync_disconnect
void mongo_sync_disconnect(mongo_sync_connection *conn)
Close and free a synchronous MongoDB connection.
Definition mongo-sync.c:396