Created
July 26, 2010 16:52
-
-
Save ctriolo/490830 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Title: tutorial.c | |
* Author: Christopher Triolo | |
* Build & Run: | |
* $ gcc -Isrc --std=c99 tutorial.c /path/to/mongo-c-driver/src/*.c -I /path/to/mongo-c-driver/src/ -o tutorial | |
* $ ./tutorial | |
* connection succeeded | |
* ... | |
* connection closed | |
*/ | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include "bson.h" | |
#include "mongo.h" | |
static void tutorial_insert_bson( mongo_connection *conn ); | |
static void tutorial_insert_batch( mongo_connection *conn ); | |
static void tutorial_count( mongo_connection *conn ); | |
static void tutorial_empty_query( mongo_connection *conn ); | |
static void tutorial_simple_query( mongo_connection *conn ); | |
static void tutorial_complex_query_sort( mongo_connection *conn ); | |
static void tutorial_complex_query_hint( mongo_connection *conn ); | |
static void tutorial_complex_query_explain( mongo_connection * conn ); | |
static void tutorial_index( mongo_connection *conn ); | |
static void tutorial_update( mongo_connection *conn); | |
int main() { | |
mongo_connection conn[1]; /* ptr */ | |
mongo_connection_options opts[1]; | |
mongo_conn_return status; | |
strcpy( opts->host , "127.0.0.1" ); | |
opts->port = 27017; | |
status = mongo_connect( conn, opts ); | |
switch (status) { | |
case mongo_conn_success: printf( "connection succeeded\n" ); break; | |
case mongo_conn_bad_arg: printf( "bad arguments\n" ); return 1; | |
case mongo_conn_no_socket: printf( "no socket\n" ); return 1; | |
case mongo_conn_fail: printf( "connection failed\n" ); return 1; | |
case mongo_conn_not_master: printf( "not master\n" ); return 1; | |
} | |
tutorial_insert_bson( conn ); | |
tutorial_insert_batch( conn ); | |
tutorial_count( conn ); | |
tutorial_index( conn ); | |
tutorial_empty_query( conn ); | |
tutorial_simple_query( conn ); | |
tutorial_complex_query_sort( conn ); | |
tutorial_complex_query_hint( conn ); | |
tutorial_complex_query_explain( conn ); | |
tutorial_update( conn ); | |
mongo_cmd_drop_db( conn, "tutorial" ); /* cleanup */ | |
mongo_destroy( conn ); | |
printf( "\nconnection closed\n" ); | |
return 0; | |
} | |
static void tutorial_insert_bson(mongo_connection *conn) { | |
bson p[1]; | |
bson_buffer p_buf[1]; | |
printf( "\nEntering tutorial_insert_bson:\n" ); | |
bson_buffer_init( p_buf ); | |
bson_append_new_oid( p_buf, "_id" ); | |
bson_append_string( p_buf, "name", "Joe" ); | |
bson_append_int( p_buf, "age", 33 ); | |
bson_from_buffer( p, p_buf ); | |
mongo_insert( conn, "tutorial.persons", p ); | |
printf( "\n" ); | |
bson_print( p ); | |
bson_destroy( p ); | |
} | |
static void tutorial_insert_batch( mongo_connection *conn ) { | |
bson *p, **ps; | |
bson_buffer *p_buf; | |
char *names[4]; | |
int ages[] = { 29, 24, 24, 32 }; | |
int i, n = 4; | |
names[0] = "Eliot"; | |
names[1] = "Mike"; | |
names[2] = "Mathias"; | |
names[3] = "Richard"; | |
printf( "\nEntering tutorial_insert_batch:\n" ); | |
ps = (bson **)malloc( sizeof( bson * ) * n); | |
for ( i = 0; i < n; i++ ) { | |
p = ( bson * )malloc( sizeof( bson ) ); | |
p_buf = ( bson_buffer * )malloc( sizeof( bson_buffer ) ); | |
bson_buffer_init( p_buf ); | |
bson_append_new_oid( p_buf, "_id" ); | |
bson_append_string( p_buf, "name", names[i] ); | |
bson_append_int( p_buf, "age", ages[i] ); | |
bson_from_buffer( p, p_buf ); | |
ps[i] = p; | |
free( p_buf ); | |
} | |
mongo_insert_batch( conn, "tutorial.persons", ps, n); | |
for ( i = 0; i < n; i++ ) { | |
printf( "\n" ); | |
bson_print( ps[i] ); | |
bson_destroy( ps[i] ); | |
free( ps[i] ); | |
} | |
} | |
static void tutorial_count( mongo_connection *conn ) { | |
int count; | |
bson empty[1]; | |
bson_empty( empty ); | |
printf( "\nEntering tutorial_count:\n" ); | |
count = mongo_count( conn, "tutorial", "persons", empty ); | |
printf( "\n\tmongo_count: %i\n", count ); | |
} | |
static void tutorial_empty_query( mongo_connection *conn) { | |
mongo_cursor *cursor; | |
bson empty[1]; | |
bson_empty( empty ); | |
printf( "\nEntering tutorial_empty_query:\n" ); | |
cursor = mongo_find( conn, "tutorial.persons", empty, empty, 0, 0, 0 ); | |
while( mongo_cursor_next( cursor ) ) { | |
printf( "\n" ); | |
bson_print( &cursor->current ); | |
} | |
mongo_cursor_destroy( cursor ); | |
bson_destroy( empty ); | |
} | |
static void tutorial_simple_query( mongo_connection *conn ) { | |
bson query[1]; | |
bson_buffer query_buf[1]; | |
mongo_cursor *cursor; | |
printf( "\nEntering tutorial_simple_query:\n" ); | |
bson_buffer_init( query_buf ); | |
bson_append_int( query_buf, "age", 24 ); | |
bson_from_buffer( query, query_buf ); | |
cursor = mongo_find( conn, "tutorial.persons", query, NULL, 0, 0, 0 ); | |
while( mongo_cursor_next( cursor ) ) { | |
bson_iterator it[1]; | |
if ( bson_find( it, &cursor->current, "name" )) { | |
printf( "\n\tname: %s\n", bson_iterator_string( it ) ); | |
} | |
} | |
bson_destroy( query ); | |
} | |
static void tutorial_complex_query_sort( mongo_connection *conn ) { | |
bson query[1]; | |
bson_buffer query_buf[1]; | |
mongo_cursor *cursor; | |
printf( "\nEntering tutorial_complex_query_sort:\n" ); | |
bson_buffer_init( query_buf ); | |
bson_append_start_object( query_buf, "$query" ); | |
bson_append_int( query_buf, "age", 24 ); | |
bson_append_finish_object( query_buf ); | |
bson_append_start_object( query_buf, "$orderby" ); | |
bson_append_int( query_buf, "name", 1); | |
bson_append_finish_object( query_buf ); | |
bson_from_buffer( query, query_buf ); | |
cursor = mongo_find( conn, "tutorial.persons", query, NULL, 0, 0, 0 ); | |
while( mongo_cursor_next( cursor ) ) { | |
printf("\n"); | |
bson_print(&cursor->current); | |
} | |
bson_destroy( query ); | |
} | |
static void tutorial_complex_query_hint( mongo_connection *conn ) { | |
bson query[1]; | |
bson_buffer query_buf[1]; | |
mongo_cursor *cursor; | |
printf( "\nEntering tutorial_complex_query_sort:\n" ); | |
bson_buffer_init( query_buf ); | |
bson_append_start_object( query_buf, "$query" ); | |
bson_append_int( query_buf, "age", 24 ); | |
bson_append_string( query_buf, "name", "Mathias" ); | |
bson_append_finish_object( query_buf ); | |
bson_append_start_object( query_buf, "$hint" ); | |
bson_append_int( query_buf, "name", 1); | |
bson_append_finish_object( query_buf ); | |
bson_from_buffer( query, query_buf ); | |
cursor = mongo_find( conn, "tutorial.persons", query, NULL, 0, 0, 0 ); | |
while( mongo_cursor_next( cursor ) ) { | |
printf("\n"); | |
bson_print(&cursor->current); | |
} | |
bson_destroy( query ); | |
} | |
static void tutorial_complex_query_explain( mongo_connection *conn ) { | |
bson query[1], out[1]; | |
bson_buffer query_buf[1]; | |
printf( "\nEntering tutorial_complex_query_explain:\n" ); | |
bson_buffer_init( query_buf ); | |
bson_append_start_object( query_buf, "$query" ); | |
bson_append_int( query_buf, "age", 33 ); | |
bson_append_finish_object( query_buf ); | |
bson_append_bool( query_buf, "$explain", 1); | |
bson_from_buffer( query, query_buf ); | |
mongo_find_one( conn, "tutorial.persons", query, NULL, out ); | |
printf( "\n" ); | |
bson_print( out ); | |
bson_destroy( out ); | |
bson_destroy( query ); | |
} | |
static void tutorial_index( mongo_connection * conn ) { | |
bson key[1]; | |
bson_buffer key_buf[1]; | |
printf( "\nEntering tutorial_index:\n" ); | |
bson_buffer_init( key_buf ); | |
bson_append_int( key_buf, "name", 1 ); | |
bson_from_buffer( key, key_buf ); | |
mongo_create_index( conn, "tutorial.persons", key, 0, NULL ); | |
bson_destroy( key ); | |
printf( "\n\tsimple index created on \"name\"\n" ); | |
bson_buffer_init( key_buf ); | |
bson_append_int( key_buf, "age", 1 ); | |
bson_append_int( key_buf, "name", 1 ); | |
bson_from_buffer( key, key_buf ); | |
mongo_create_index( conn, "tutorial.persons", key, 0, NULL ); | |
bson_destroy( key ); | |
printf( "\n\tcompound index created on \"age, name\"\n" ); | |
} | |
static void tutorial_update( mongo_connection *conn ) { | |
bson cond[1], op[1], out[1]; | |
bson_buffer cond_buf[1], op_buf[1]; | |
printf( "\nEntering tutorial_update:\n" ); | |
bson_buffer_init( cond_buf ); | |
bson_append_string( cond_buf, "name", "Joe"); | |
bson_append_int( cond_buf, "age", 33); | |
bson_from_buffer( cond, cond_buf ); | |
mongo_find_one( conn, "tutorial.persons", cond, NULL, out ); | |
printf( "\n" ); | |
bson_print( out ); | |
bson_destroy( out ); | |
bson_buffer_init( op_buf ); | |
bson_append_start_object( op_buf, "$inc" ); | |
bson_append_int( op_buf, "visits", 1 ); | |
bson_append_finish_object( op_buf ); | |
bson_from_buffer( op, op_buf ); | |
mongo_update(conn, "tutorial.persons", cond, op, 0); | |
mongo_find_one( conn, "tutorial.persons", cond, NULL, out ); | |
printf( "\n" ); | |
bson_print( out ); | |
bson_destroy( out ); | |
bson_destroy( cond ); | |
bson_destroy( op ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment