I’m a novice user for MongoDB using C driver, and I can’t find any detail tutorial that teach how to create my first MongoDB program in C.
I’ve created my first program according to http://api.mongodb.org/c/current/tutorial.html
#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;
}
However, it shows up an error that it can not find where “mongo.h” is.
Does anyone know how to compile this file so I can link it to the MongoDB C driver?
You should go to C Language Driver docs and download the latest stable code base (v0.4).
This contains mongo.h. Install it where-ever you want on your computer, and build the library. You then need to specify the -I to the location of your downloaded headers, and -L for your compiled library.