Skip to content

Instantly share code, notes, and snippets.

@banker
Forked from anonymous/mongoc
Created January 6, 2012 22:17
Show Gist options
  • Save banker/1572678 to your computer and use it in GitHub Desktop.
Save banker/1572678 to your computer and use it in GitHub Desktop.
MongoDB connection via C driver
#include <stdio.h>
#include "mongo.h"
int main() {
mongo conn[1];
int status = mongo_connect( conn, "127.0.0.1", 27017 );
if( status != MONGO_OK ) {
switch ( conn->err ) {
case MONGO_CONN_SUCCESS: printf( "connection succeeded\n" ); break;
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;
}
}
mongo_destroy( conn );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment