반응형
#include <bson.h>
#include <mongoc.h>
#include <stdio.h>
int main (int argc, char *argv[]){
mongoc_client_t *client;
mongoc_collection_t *collection;
bson_error_t error;
bson_oid_t oid;
struct tm born = { 0 };
bson_t *document;
born.tm_year = 6;
born.tm_mon = 11;
born.tm_mday = 9;
mongoc_init ();
client = mongoc_client_new ("mongodb://localhost:27017");
collection = mongoc_client_get_collection (client, "attendance", "userlist");
document = BCON_NEW (
"born", BCON_DATE_TIME (mktime (&born) * 1000),
"name", "{",
"choi", BCON_UTF8 ("check"),
"kim", BCON_UTF8 ("nope"),
"}"
);
bson_oid_init (&oid, NULL); //ObjectId인듯 하다. 안해줘도 무방하다.
BSON_APPEND_OID (document, "_id", &oid);
// 만든 document 대로 해당 db로 insert해주게 된다.
if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, document, NULL, &error)) {
fprintf (stderr, "%s\n", error.message);
}
bson_destroy (document);
mongoc_collection_destroy (collection);
mongoc_client_destroy (client);
mongoc_cleanup ();
return 0;
}
#include <bson.h>
#include <mongoc.h>
#include <stdio.h>
intmain (int argc, char *argv[])
{
mongoc_client_t *client;
mongoc_collection_t *collection;
mongoc_cursor_t *cursor;
const bson_t *doc;
bson_t *query;
char *str;
mongoc_init ();
client =
mongoc_client_new ("mongodb://localhost:27017");
collection = mongoc_client_get_collection (client, "attendance", "userlist");
query = bson_new (); // 밑에의 조건을 추가 안해주면 모든 데이터 출력 해줌
//BSON_APPEND_UTF8(query,"hello", "world"); // 특정 키:값인 데이터 찾기
cursor = mongoc_collection_find_with_opts (collection, query, NULL, NULL);
while (mongoc_cursor_next (cursor, &doc)) {
str = bson_as_json (doc, NULL);
printf ("%s\n", str);
bson_free (str);
}
bson_destroy (query);
mongoc_cursor_destroy (cursor);
mongoc_collection_destroy (collection);
mongoc_client_destroy (client);
mongoc_cleanup ();
return 0;
}