* mongo-c-driver 1.6.3 버전 사용

* $ : 일반 사용자 계정 , # : 루트 계정


[mongo-c-driver Github]

https://github.com/mongodb/mongo-c-driver



[설치]

(원하시는 위치에서 진행해 주세요.)

$ wget https://github.com/mongodb/mongo-c-driver/releases/download/1.6.3/mongo-c-driver-1.6.3.tar.gz

$ tar xzf mongo-c-driver-1.6.3.tar.gz

$ cd mongo-c-driver-1.6.3

$ ./configure --disable-automatic-init-and-cleanup

$ make

$ sudo make install



[확인]


1) /usr/local/include/libmongoc-1.0


2) /usr/local/include/libbson-1.0




[참조방법 예시]


1) gcc -o mongo-c-test mongo-c-test.c -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/libbson-1.0 -L/usr/local/lib/ -L/usr/lib64 -lmongoc-1.0 -lbson-1.0


2) makefile

--------------------------------

INCMONGO = -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/libbson-1.0

LIBMONGO = -L/usr/local/lib/ -L/usr/lib64

LIBS    = -lmongoc-1.0 -lbson-1.0

CFLAGS  = $(INCMONGO) $(LIBMONGO) $(LIBS)

TARGET  = mongo-c-test


all: $(TARGET)


$(TARGET):

        $(CC) -o $@ $@.c $(CFLAGS) 


clean:

        rm -f $(TARGET)

--------------------------------

(예시일뿐 필요한 내용으로 수정하셔야 합니다.)



[예제 소스]

* DB 풀 사용법이 포함된 소스입니다.



#include <bcon.h>
#include <mongoc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define SERVER "mongodb://url"
#define DATABASE "MONGO_TEST"

int main(int argc, char *argv[])
{
mongoc_client_t *client;
mongoc_collection_t *collection;
//mongoc_index_opt_t opt;
bson_t *doc;
bson_error_t error;

mongoc_client_pool_t *pool;
mongoc_uri_t *uri; // 몽고DB 서버주소

mongoc_init();

uri = mongoc_uri_new(SERVER);
pool = mongoc_client_pool_new(uri);

mongoc_client_pool_set_error_api(pool, 2);
mongoc_client_pool_max_size(pool, 2); // DB풀에 DB클라이언트 2개

client = mongoc_client_pool_pop(pool);
collection = mongoc_client_get_collection(client, DATABASE, "TEST");

int temp = 0;
while (true)
{

if (temp < 100)
{
temp++;
printf("%d\n", temp);

doc = bson_new();
BSON_APPEND_UTF8 (doc, "user", "user2");
BSON_APPEND_INT32(doc, "temp_idx", temp);
if (!mongoc_collection_insert(collection, MONGOC_INSERT_NONE, doc, NULL, &error))
{
fprintf(stderr, "%s\n", error.message);
}
char *str;
str = bson_as_json(doc, NULL);
printf("doc : %s\n", str);
bson_free(str);
bson_destroy(doc);

}
else
{
break;
}
usleep(100);
}
mongoc_client_pool_push(pool, client);
mongoc_client_pool_destroy(pool);
mongoc_uri_destroy(uri);
mongoc_cleanup();

return 0;
}


- 결과

1) 콘솔


2) robomongo




---

+ Recent posts