前言
现在看mongoc的手册, 是因为同事建议将mogoc++接口换成mongoc接口. 说c++接口中有些特性没有, c接口中有.
今天将mongoc手册和mongoc源码中试验相关的API用法例子试验浏览了一遍. 个人感觉, 即使是mongoc++接口中没有的特性, 也可以将需要的元素指针由类方法引出来, 然后调用mongoC接口完成. 不用将MongoC++接口全部换成mongoC接口. 因为mongoC++接口是对mongoC接口进行了封装, 用的东西是一样的. 只不过类管理数据更好, 更易用.
以前没用过NoSQL数据库, 刚开始看mongoC接口手册和源码, 和要解决的问题掺在一起, 让我蒙逼了好几天.其实正确思路是:直接看mongoC手册和源码, 将要解决的问题先放一下, 将手册中的mongoC操作先看明白了. e.g. 数据库打开关闭, 数据增删改查搞清楚, 做了试验. 再解决像建立索引等问题.
其他人用mongoC遇到的问题, 我也会遇到. 其他人用了长时间解决的问题, 我也会花时间解决, 没有捷径. 所以还是要将一个生东西基本的概念和用法看懂, 再解决问题, 这样来的快. 如果要吃5个包子才能饱, 那前面4个包子是必须要吃的.
人家源码中为了测试API是否正确, 都写了测试用例, 看人家怎么用, 照着用就行. 东西都是人家做的, 不照着人家用法搞, 也不好使啊:) 等再有时间, 我要自己从头搭mongoDB环境, 从头玩一遍mongodb:P 今天解决的最后一个问题, 如何对打开的集合建立索引. 建立索引最简单的是用mongoc_collection_ensure_index. mongoc_collection_ensure_index用了mongoc_collection_create_index. mongoc_collection_create_index用了mongoc_collection_create_index_with_opts mongoc_collection_create_index_with_opts真正的建立索引. mongoc++接口中有.ensureIndex, 应该也用的是mongoc_collection_ensure_index, 虽然我现在还没看mongoC++接口, 他要不用mongoc_collection_ensure_index, 也实现不了建立索引.
最终的demo工程
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <cstddef>
#include
"MongoDbEx.h"
#include
"BusinessLogic.h"
int testcase_mongoc_example_command_simple(int argc, char* argv[]);
int testcase_mongoc_example_document_add(int argc, char* argv[]);
int testcase_mongoc_example_document_del(int argc, char* argv[]);
int testcase_mongoc_example_document_modify(int argc, char* argv[]);
int testcase_mongoc_example_document_query_all(int argc, char* argv[]);
int testcase_mongoc_example_document_query_special(int argc, char* argv[]);
int testcase_mongoc_example_document_query_special_count(int argc, char* argv[]);
int testcase_mongoc_example_create_Index(int argc, char* argv[]);
int main(int argc, char* argv[])
{
printf(
"============================================================\n");
printf(
">> testcase v1.0.0.8\n");
printf(
"============================================================\n");
testcase_mongoc_example_create_Index(argc, argv);
printf(
"============================================================\n");
printf(
"END\n");
printf(
"============================================================\n");
return 0;
}
int testcase_mongoc_example_create_Index(int argc, char* argv[])
{
bson_t keys;
mongoc_index_opt_t opt;
bson_error_t error;
bool r;
mongoc_client_t* client = NULL;
mongoc_collection_t* collection = NULL;
do {
mongoc_init();
client = mongoc_client_new(
"mongodb://localhost:27017/?appname=find-specific-example");
collection = mongoc_client_get_collection(client,
"db_ls",
"coll_ls");
bson_init(&keys);
bson_append_int32(&keys,
"birthday", -
1,
1);
mongoc_index_opt_init(&opt);
opt.unique =
1;
r = mongoc_collection_create_index(collection, &keys, &opt, &error);
if (!r) {
ShowErrMsg(
"mongoc_collection_create_index", &error);
break;
}
else {
printf(
"mongoc_collection_create_index ok\n");
}
/** run result
mongoc_collection_create_index ok
*/
}
while (
0);
bson_destroy(&keys);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
mongoc_cleanup();
return 0;
}
int testcase_mongoc_example_document_query_special_count(int argc, char* argv[])
{
mongoc_client_t* client = NULL;
mongoc_collection_t* collection = NULL;
const bson_t* doc = NULL;
bson_t* query = NULL;
bson_t child = BSON_INITIALIZER;
long lIndex =
0;
char szBuf[
260] = {'\
0'};
int64_t count =
0;
bson_error_t error = {
0};
char* uri_str = NULL;
mongoc_init();
client = mongoc_client_new(
"mongodb://localhost:27017/?appname=find-specific-example");
collection = mongoc_client_get_collection(client,
"db_ls",
"coll_ls");
query = bson_new();
bson_append_document_begin(query,
"name", -
1, &child);
BSON_APPEND_UTF8(&child,
"first",
"lostspeed258");
BSON_APPEND_UTF8(&child,
"last",
"hk");
bson_append_document_end(query, &child);
count = mongoc_collection_count(collection, MONGOC_QUERY_NONE, query,
0,
0, NULL, &error);
if (count <
0) {
ShowErrMsg(
"mongoc_collection_count", &error);
}
else {
uri_str = bson_strdup_printf(
"mongoc_collection_count = 0x%x\n",
(long long)count);
printf(
"%s", uri_str);
/** run result
mongoc_collection_count =
0x1
*/
}
bson_free(uri_str);
bson_destroy(query);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
mongoc_cleanup();
return 0;
}
int testcase_mongoc_example_document_del(int argc, char* argv[])
{
mongoc_client_t* client;
mongoc_collection_t* collection;
mongoc_cursor_t* cursor;
const bson_t* doc;
bson_t* query = NULL;
bson_t del = BSON_INITIALIZER;
bson_t child;
bson_iter_t iter;
bson_error_t error;
long lIndex =
0;
char szBuf[
260] = {'\
0'};
mongoc_init();
client = mongoc_client_new(
"mongodb://localhost:27017/?appname=find-specific-example");
collection = mongoc_client_get_collection(client,
"db_ls",
"coll_ls");
query = bson_new();
bson_append_document_begin(query,
"name", -
1, &child);
BSON_APPEND_UTF8(&child,
"first",
"lostspeed5");
BSON_APPEND_UTF8(&child,
"last",
"cn");
bson_append_document_end(query, &child);
cursor = mongoc_collection_find_with_opts(collection, query, NULL, NULL);
while (mongoc_cursor_next(cursor, &doc)) {
sprintf(szBuf,
"find [%ld]\0", lIndex++);
ShowDocument(szBuf, (bson_t*)doc);
if (bson_iter_init_find(&iter, doc,
"_id")) {
bson_append_value(&del, bson_iter_key(&iter), -
1, bson_iter_value(&iter));
if (!mongoc_collection_remove(collection, MONGOC_REMOVE_SINGLE_REMOVE, &del, NULL, &error)) {
ShowErrMsg(
"mongoc_collection_update", &error);
}
else {
printf(
"del ok\n");
}
}
}
bson_destroy(query);
bson_destroy(&del);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
mongoc_cleanup();
}
int testcase_mongoc_example_document_modify(int argc, char* argv[])
{
mongoc_client_t* client;
mongoc_collection_t* collection;
mongoc_cursor_t* cursor;
const bson_t* doc;
bson_t* query;
bson_t* modify;
bson_t child;
bson_error_t error;
long lIndex =
0;
char szBuf[
260] = {'\
0'};
mongoc_init();
client = mongoc_client_new(
"mongodb://localhost:27017/?appname=find-specific-example");
collection = mongoc_client_get_collection(client,
"db_ls",
"coll_ls");
query = bson_new();
bson_append_document_begin(query,
"name", -
1, &child);
BSON_APPEND_UTF8(&child,
"first",
"lostspeed3");
BSON_APPEND_UTF8(&child,
"last",
"cn");
bson_append_document_end(query, &child);
modify = bson_new();
bson_append_document_begin(modify,
"name", -
1, &child);
BSON_APPEND_UTF8(&child,
"first",
"lostspeed258");
BSON_APPEND_UTF8(&child,
"last",
"hk");
bson_append_document_end(modify, &child);
cursor = mongoc_collection_find_with_opts(collection, query, NULL, NULL);
while (mongoc_cursor_next(cursor, &doc)) {
sprintf(szBuf,
"find [%ld]\0", lIndex++);
ShowDocument(szBuf, (bson_t*)doc);
if (!mongoc_collection_update(collection, MONGOC_UPDATE_NONE, query, modify, NULL, &error)) {
ShowErrMsg(
"mongoc_collection_update", &error);
}
else {
printf(
"modify ok\n");
}
break;
}
bson_destroy(modify);
bson_destroy(query);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
mongoc_cleanup();
}
int testcase_mongoc_example_document_query_special(int argc, char* argv[])
{
mongoc_client_t* client;
mongoc_collection_t* collection;
mongoc_cursor_t* cursor;
const bson_t* doc;
bson_t* query;
bson_t child;
long lIndex =
0;
char szBuf[
260] = {'\
0'};
mongoc_init();
client = mongoc_client_new(
"mongodb://localhost:27017/?appname=find-specific-example");
collection = mongoc_client_get_collection(client,
"db_ls",
"coll_ls");
query = bson_new();
bson_append_document_begin(query,
"name", -
1, &child);
BSON_APPEND_UTF8(&child,
"first",
"lostspeed3");
BSON_APPEND_UTF8(&child,
"last",
"cn");
bson_append_document_end(query, &child);
cursor = mongoc_collection_find_with_opts(collection, query, NULL, NULL);
while (mongoc_cursor_next(cursor, &doc)) {
sprintf(szBuf,
"find [%ld]\0", lIndex++);
ShowDocument(szBuf, (bson_t*)doc);
/**
[find [
0]]
{
"_id" : {
"$oid" :
"58e351936e955221a1689462" },
"name" : {
"first" :
"lostspeed3",
"last" :
"cn" },
"birthday" : {
"$date" :
2234350468000 },
"skill" : [
"C",
"C++",
"MASM",
"RE" ],
"degrees" : [
{
"school" :
"zhongnian",
"degree" :
"graduates" },
{
"school" :
"cr",
"degree" :
"graduates" } ] }
*/
}
bson_destroy(query);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
mongoc_cleanup();
}
int testcase_mongoc_example_document_query_all(int argc, char* argv[])
{
mongoc_client_t* client = NULL;
mongoc_collection_t* collection = NULL;
mongoc_cursor_t* cursor = NULL;
const bson_t* doc = NULL;
bson_t* query = NULL;
long lIndex =
0;
char szBuf[
260] = {'\
0'};
mongoc_init();
client = mongoc_client_new(
"mongodb://localhost:27017/?appname=find-example");
collection = mongoc_client_get_collection(client,
"db_ls",
"coll_ls");
query = bson_new();
cursor = mongoc_collection_find_with_opts(collection, query, NULL, NULL);
while (mongoc_cursor_next(cursor, &doc)) {
sprintf(szBuf,
"%ld\0", lIndex++) ;
ShowDocument(szBuf, (bson_t*)doc);
}
bson_destroy(query);
mongoc_cursor_destroy(cursor);
mongoc_collection_destroy(collection);
mongoc_client_destroy(client);
mongoc_cleanup();
return 0;
}
int testcase_mongoc_example_document_add(int argc, char* argv[])
{
bool bRc =
false;
TAG_PERSON_INFO person;
bson_t* document = NULL;
mongoc_client_t* client = NULL;
mongoc_database_t* database = NULL;
mongoc_collection_t* collection = NULL;
bson_error_t error;
bool retval =
false;
do {
memset(&person,
0, sizeof(person));
mongoc_init();
client = mongoc_client_new(
"mongodb://localhost:27017");
mongoc_client_set_appname(client,
"CRUD-Add");
database = mongoc_client_get_database(client,
"db_ls");
collection = mongoc_client_get_collection(client,
"db_ls",
"coll_ls");
retval = DbOptExec_command_simple(client,
"admin",
"ping", &error);
if (!retval) {
ShowErrMsg(
"ping", &error);
break;
}
setTime(&person.birthday,
1973,
4,
1,
13,
30,
50);
person.name.first =
"lostspeed5";
person.name.last =
"cn";
person.skillAry[
0] =
"C";
person.skillAry[
1] =
"C++";
person.skillAry[
2] =
"MASM";
person.skillAry[
3] =
"RE";
person.skillAry[
4] = NULL;
person.degreeAry[
0].degree =
"graduates";
person.degreeAry[
0].school =
"zhongnian";
person.degreeAry[
1].degree =
"graduates";
person.degreeAry[
1].school =
"cr";
person.degreeAry[
2].degree = NULL;
person.degreeAry[
2].school = NULL;
document = bson_new();
fill_TAG_PERSON_INFO(document, &person);
ShowDocument(
"person", document);
/**
[person] {
"name" : {
"first" :
"lostspeed",
"last" :
"cn" },
"birthday" : {
"$date" :
1404690916000 } }
[person] {
"name" : {
"first" :
"lostspeed",
"last" :
"cn" },
"birthday" : {
"$date" : -
1211776988000 },
"skill" : [
"C",
"C++",
"MASM",
"RE" ]
}
[person] {
"name" : {
"first" :
"lostspeed",
"last" :
"cn" },
"birthday" : {
"$date" :
2234350468000 },
"skill" : [
"C",
"C++",
"MASM",
"RE" ],
"degrees" : [
{
"school" :
"zhongnian",
"degree" :
"graduates" },
{
"school" :
"cr",
"degree" :
"graduates" } ] }
[person] {
"name" : {
"first" :
"lostspeed",
"last" :
"cn" },
"birthday" : {
"$date" :
105087610000 },
"skill" : [
"C",
"C++",
"MASM",
"RE" ],
"degrees" : [
{
"school" :
"zhongnian",
"degree" :
"graduates" },
{
"school" :
"cr",
"degree" :
"graduates" } ]
}
*/
bRc = DbOptAddDocument(collection, document, &error);
if (bRc) {
printf(
"CURD-add ok\n");
}
else {
ShowErrMsg(
"CURD-add", &error);
}
}
while (
0);
if (NULL != document) {
bson_destroy(document);
}
if (NULL != collection) {
mongoc_collection_destroy(collection);
}
if (NULL != database) {
mongoc_database_destroy(database);
}
if (NULL != client) {
mongoc_client_destroy(client);
}
return 0;
}
int testcase_mongoc_example_command_simple(int argc, char* argv[])
{
mongoc_client_t* client = NULL;
mongoc_database_t* database = NULL;
mongoc_collection_t* collection = NULL;
bson_t* insert = NULL;
bson_error_t error;
bool retval =
false;
mongoc_init();
client = mongoc_client_new(
"mongodb://localhost:27017");
/*
* Register the application name so we can track it
in the profile logs
* on the server. This can also be
done from the URI (see other examples).
*/
mongoc_client_set_appname(client,
"connect-example");
database = mongoc_client_get_database(client,
"db_name");
collection = mongoc_client_get_collection(client,
"db_name",
"coll_name");
retval = DbOptExec_command_simple(client,
"admin",
"ping", &error);
if (!retval) {
ShowErrMsg(
"ping", &error);
return EXIT_FAILURE;
}
/**
[ping] {
"ok" :
1.0 }
*/
insert = BCON_NEW(
"hello", BCON_UTF8(
"world"));
if (!mongoc_collection_insert(
collection, MONGOC_INSERT_NONE, insert, NULL, &error)) {
ShowErrMsg(
"mongoc_collection_insert", &error);
}
bson_destroy(insert);
retval = DbOptExec_command_simple(client,
"admin",
"buildinfo", &error);
if (!retval) {
ShowErrMsg(
"buildinfo", &error);
}
/**
[buildinfo] {
"version" :
"2.6.10",
"gitVersion" :
"5901dbfb49d16eaef6f2c2c50fba534d23ac7f6c",
"OpenSSLVersion" :
"",
"sysInfo" :
"Linux build18.nj1.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49",
"loaderFlags" :
"-fPIC -pthread -Wl,-z,now -rdynamic",
"compilerFlags" :
"-Wnon-virtual-dtor -Woverloaded-virtual -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -pipe -Werror -O3 -Wno-unused-function -Wno-deprecated-declarations -fno-builtin-memcmp",
"allocator" :
"tcmalloc",
"versionArray" : [
2,
6,
10,
0 ],
"javascriptEngine" :
"V8",
"bits" :
64,
"debug" :
false,
"maxBsonObjectSize" :
16777216,
"ok" :
1.0 }
*/
retval = DbOptExec_command_simple(client,
"admin",
"serverStatus", &error);
if (!retval) {
ShowErrMsg(
"serverStatus", &error);
}
/**
[serverStatus]
{
"host" :
"debian",
"version" :
"2.6.10",
"process" :
"mongod",
"pid" :
3243,
"uptime" :
21899.0,
"uptimeMillis" :
21897854,
"uptimeEstimate" :
19920.0,
"localTime" : {
"$date" :
1491227764737 },
"asserts" : {
"regular" :
0,
"warning" :
0,
"msg" :
0,
"user" :
0,
"rollovers" :
0 },
"backgroundFlushing" : {
"flushes" :
364,
"total_ms" :
54,
"average_ms" :
0.14835164835164835195,
"last_ms" :
0,
"last_finished" : {
"$date" :
1491227708337 } },
"connections" : {
"current" :
1,
"available" :
818,
"totalCreated" :
9 },
"cursors" : {
"note" :
"deprecated, use server status metrics",
"clientCursors_size" :
0,
"totalOpen" :
0,
"pinned" :
0,
"totalNoTimeout" :
0,
"timedOut" :
0 },
"dur" : {
"commits" :
29,
"journaledMB" :
0.0,
"writeToDataFilesMB" :
0.0,
"compression" :
0.0,
"commitsInWriteLock" :
0,
"earlyCommits" :
0,
"timeMs" : {
"dt" :
3044,
"prepLogBuffer" :
0,
"writeToJournal" :
0,
"writeToDataFiles" :
0,
"remapPrivateView" :
0 } },
"extra_info" : {
"note" :
"fields vary by platform",
"heap_usage_bytes" :
62725408,
"page_faults" :
98 },
"globalLock" : {
"totalTime" :
21897856000,
"lockTime" :
5334268,
"currentQueue" : {
"total" :
0,
"readers" :
0,
"writers" :
0 },
"activeClients" : {
"total" :
0,
"readers" :
0,
"writers" :
0 } },
"indexCounters" : {
"accesses" :
19,
"hits" :
19,
"misses" :
0,
"resets" :
0,
"missRatio" :
0.0 },
"locks" : {
"." : {
"timeLockedMicros" : {
"R" :
435761,
"W" :
5334268 },
"timeAcquiringMicros" : {
"R" :
4899977,
"W" :
2994540 } },
"admin" : {
"timeLockedMicros" : {
"r" :
151982,
"w" :
0 },
"timeAcquiringMicros" : {
"r" :
369253,
"w" :
0 } },
"local" : {
"timeLockedMicros" : {
"r" :
128739,
"w" :
83 },
"timeAcquiringMicros" : {
"r" :
54838,
"w" :
1 } },
"testdb" : {
"timeLockedMicros" : {
"r" :
2081468,
"w" :
130 },
"timeAcquiringMicros" : {
"r" :
19698,
"w" :
3 } },
"mongodb" : {
"timeLockedMicros" : {
"r" :
191151,
"w" :
128 },
"timeAcquiringMicros" : {
"r" :
30413,
"w" :
4 } },
"db_name" : {
"timeLockedMicros" : {
"r" :
375654,
"w" :
908 },
"timeAcquiringMicros" : {
"r" :
14419,
"w" :
40 } },
"Barca" : {
"timeLockedMicros" : {
"r" :
158307,
"w" :
134 },
"timeAcquiringMicros" : {
"r" :
19699,
"w" :
3 } },
"dbs" : {
"timeLockedMicros" : {
"r" :
610389,
"w" :
224 },
"timeAcquiringMicros" : {
"r" :
17663,
"w" :
6 } },
"test1" : {
"timeLockedMicros" : {
"r" :
829203,
"w" :
28 },
"timeAcquiringMicros" : {
"r" :
15951,
"w" :
2 } },
"yekai" : {
"timeLockedMicros" : {
"r" :
2065973,
"w" :
269 },
"timeAcquiringMicros" : {
"r" :
61179,
"w" :
4 } },
"tutorial" : {
"timeLockedMicros" : {
"r" :
1491259,
"w" :
157 },
"timeAcquiringMicros" : {
"r" :
28555,
"w" :
3 } } },
"network" : {
"bytesIn" :
5730,
"bytesOut" :
13310,
"numRequests" :
44 },
"opcounters" : {
"insert" :
9,
"query" :
3641,
"update" :
0,
"delete" :
0,
"getmore" :
0,
"command" :
38 },
"opcountersRepl" : {
"insert" :
0,
"query" :
0,
"update" :
0,
"delete" :
0,
"getmore" :
0,
"command" :
0 },
"recordStats" : {
"accessesNotInMemory" :
0,
"pageFaultExceptionsThrown" :
0,
"Barca" : {
"accessesNotInMemory" :
0,
"pageFaultExceptionsThrown" :
0 },
"admin" : {
"accessesNotInMemory" :
0,
"pageFaultExceptionsThrown" :
0 },
"db_name" : {
"accessesNotInMemory" :
0,
"pageFaultExceptionsThrown" :
0 },
"dbs" : {
"accessesNotInMemory" :
0,
"pageFaultExceptionsThrown" :
0 },
"local" : {
"accessesNotInMemory" :
0,
"pageFaultExceptionsThrown" :
0 },
"mongodb" : {
"accessesNotInMemory" :
0,
"pageFaultExceptionsThrown" :
0 },
"test1" : {
"accessesNotInMemory" :
0,
"pageFaultExceptionsThrown" :
0 },
"testdb" : {
"accessesNotInMemory" :
0,
"pageFaultExceptionsThrown" :
0 },
"tutorial" : {
"accessesNotInMemory" :
0,
"pageFaultExceptionsThrown" :
0 },
"yekai" : {
"accessesNotInMemory" :
0,
"pageFaultExceptionsThrown" :
0 } },
"writeBacksQueued" :
false,
"mem" : {
"bits" :
64,
"resident" :
158,
"virtual" :
62683,
"supported" :
true,
"mapped" :
31251,
"mappedWithJournal" :
62502 },
"metrics" : {
"cursor" : {
"timedOut" :
0,
"open" : {
"noTimeout" :
0,
"pinned" :
0,
"total" :
0 } },
"document" : {
"deleted" :
0,
"inserted" :
9,
"returned" :
0,
"updated" :
0 },
"getLastError" : {
"wtime" : {
"num" :
0,
"totalMillis" :
0 },
"wtimeouts" :
0 },
"operation" : {
"fastmod" :
0,
"idhack" :
0,
"scanAndOrder" :
0 },
"queryExecutor" : {
"scanned" :
0,
"scannedObjects" :
0 },
"record" : {
"moves" :
0 },
"repl" : {
"apply" : {
"batches" : {
"num" :
0,
"totalMillis" :
0 },
"ops" :
0 },
"buffer" : {
"count" :
0,
"maxSizeBytes" :
268435456,
"sizeBytes" :
0 },
"network" : {
"bytes" :
0,
"getmores" : {
"num" :
0,
"totalMillis" :
0 },
"ops" :
0,
"readersCreated" :
0 },
"preload" : {
"docs" : {
"num" :
0,
"totalMillis" :
0 },
"indexes" : {
"num" :
0,
"totalMillis" :
0 } } },
"storage" : {
"freelist" : {
"search" : {
"bucketExhausted" :
0,
"requests" :
8,
"scanned" :
16 } } },
"ttl" : {
"deletedDocuments" :
0,
"passes" :
364 } },
"ok" :
1.0 }
*/
retval = DbOptExec_command_simple(client,
"admin",
"ismaster", &error);
if (!retval) {
ShowErrMsg(
"ismaster", &error);
}
/**
[ismaster] {
"ismaster" :
true,
"maxBsonObjectSize" :
16777216,
"maxMessageSizeBytes" :
48000000,
"maxWriteBatchSize" :
1000,
"localTime" : {
"$date" :
1491230240360 },
"maxWireVersion" :
2,
"minWireVersion" :
0,
"ok" :
1.0
}
*/
retval = DbOptExec_command_simple(client,
"admin",
"getlasterror", &error);
if (!retval) {
ShowErrMsg(
"getlasterror", &error);
}
/**
[getlasterror] {
"connectionId" :
11,
"n" :
0,
"syncMillis" :
0,
"writtenTo" :
null,
"err" :
null,
"ok" :
1.0
}
*/
retval = DbOptExec_command_simple(client,
"admin",
"foo", &error);
if (!retval) {
ShowErrMsg(
"foo", &error);
}
/**
[foo] error : no such cmd: foo
*/
retval = DbOptExec_command_simple(client,
"admin",
"", &error);
if (!retval) {
ShowErrMsg(
"", &error);
}
/**
[] error : no such cmd:
*/
retval = DbOptExec_command_simple(client,
"admin",
"db", &error);
if (!retval) {
ShowErrMsg(
"db", &error);
}
/**
[db] error : no such cmd: db
*/
/**
MongoDB shell version:
2.6.10
connecting
to: test
> show dbs
Barca
0.078GB
admin
0.078GB
db_name
0.078GB
local
0.078GB
mongodb
0.078GB
test1
0.078GB
testdb
0.078GB
tutorial
0.078GB
yekai
7.950GB
>
>
use test1
switched
to db test1
> show collections
system.indexes
test1
>
*/
retval = DbOptExec_command_simple(client,
"admin",
"use",
"test1", &error);
if (!retval) {
ShowErrMsg(
"use test1", &error);
}
/**
[
use test1] error : no such cmd:
use
*/
retval = DbOptExec_command_simple(client,
"test1",
"drop",
"test1", &error);
if (!retval) {
ShowErrMsg(
"drop test1", &error);
}
/**
[drop] {
"ns" :
"test1.test1",
"nIndexesWas" :
1,
"ok" :
1.0 }
*/
/** mongo console
>
use test1
switched
to db test1
> show collections
system.indexes
>
*/
retval = DbOptExec_command_simple(client,
"admin",
"getCmdLineOpts", &error);
if (!retval) {
ShowErrMsg(
"getCmdLineOpts", &error);
}
/**
[getCmdLineOpts] {
"argv" : [
"\/usr\/local\/mongodb\/bin\/mongod",
"--config",
"\/usr\/local\/mongodb\/bin\/mongodb.conf" ],
"parsed" : {
"config" :
"\/usr\/local\/mongodb\/bin\/mongodb.conf",
"net" : {
"http" : {
"enabled" :
false },
"port" :
27017 },
"processManagement" : {
"fork" :
true },
"storage" : {
"dbPath" :
"\/usr\/local\/mongodb\/db" },
"systemLog" : {
"destination" :
"file",
"path" :
"\/usr\/local\/mongodb\/logs\/mongodb.log" }
},
"ok" :
1.0
}
*/
mongoc_collection_destroy(collection);
mongoc_database_destroy(database);
mongoc_client_destroy(client);
mongoc_cleanup();
return 0;
}
#if !defined(AFX_BUSINESSLOGIC_H__91436BA7_97DD_495E_A750_A141145804E1__INCLUDED_)
#define AFX_BUSINESSLOGIC_H__91436BA7_97DD_495E_A750_A141145804E1__INCLUDED_
#include <bson.h>
#include <bcon.h>
#include <mongoc.h>
#ifndef BOOL
typedef
bool BOOL;
#define TRUE 1
#define FALSE 0
#endif // #ifndef BOOL
typedef
struct _tag_name {
const char* first;
const char* last;
} TAG_NAME;
typedef
struct _tag_degress {
const char* school;
const char* degree;
} TAG_DEGRESS;
typedef
struct _tag_person_info {
TAG_NAME name;
struct tm birthday;
const char* skillAry[
10];
TAG_DEGRESS degreeAry[
10];
} TAG_PERSON_INFO;
void fill_TAG_PERSON_INFO(bson_t* document, TAG_PERSON_INFO* person);
#endif // !defined(AFX_BUSINESSLOGIC_H__91436BA7_97DD_495E_A750_A141145804E1__INCLUDED_)
#ifndef __MONGODB_EX__
#define __MONGODB_EX__
#include <bson.h>
#include <bcon.h>
#include <mongoc.h>
#ifndef BOOL
typedef
bool BOOL;
#define TRUE 1
#define FALSE 0
#endif // #ifndef BOOL
BOOL DbOptInit();
BOOL DbOptUnInit();
mongoc_client_t* DbOptOpenConect(
const char* pConnectUrl);
void DbOptCloseConect(mongoc_client_t*& pDbConnect);
mongoc_database_t* DbOptOpenDb(mongoc_client_t* pDbConnect,
const char* pDbName);
void DbOptCloseDb(mongoc_database_t*& pDb);
mongoc_collection_t* DbOptOpenTbl(mongoc_database_t* pDb,
const char* pTblName);
void DbOptCloseTbl(mongoc_collection_t*& pTbl);
bool DbOptExec_command_simple(
mongoc_client_t* pClient,
const char* pcDbName,
const char* pcCommand,
bson_error_t* pError);
bool DbOptExec_command_simple(
mongoc_client_t* pClient,
const char* pcDbName,
const char* pcCommand,
const char* pcValue,
bson_error_t* pError);
bool DbOptAddDocument(mongoc_collection_t*& pTbl, bson_t* document, bson_error_t* error);
void ShowErrMsg(
const char* pTip, bson_error_t* error);
void ShowReply(
const char* pTip, bson_t* reply);
void ShowDocument(
const char* pTip, bson_t* document);
bson_t* new_formatv_bson(
const char* json, ...);
char* single_quotes_to_double(
const char* str);
void setTime(
struct tm* ptm,
int year,
int month,
int day,
int hour,
int min,
int sec);
void show_tm(
const char* pTip,
struct tm* ptm);
#endif // #ifndef __MONGODB_EX__
#include "BusinessLogic.h"
#include "MongoDbEx.h"
void fill_TAG_PERSON_INFO(bson_t* document, TAG_PERSON_INFO* person)
{
int i =
0;
int iArySize =
0;
bson_t child;
bson_t child2;
const char* key = NULL;
size_t keylen =
0;
char buf[
32] = {
'\0'};
if ((NULL == document) || (NULL == person)) {
return;
}
bson_append_document_begin(document,
"name", -
1, &child);
BSON_APPEND_UTF8(&child,
"first", person->name.first);
BSON_APPEND_UTF8(&child,
"last", person->name.last);
bson_append_document_end(document, &child);
show_tm(
"person->birthday", &person->birthday);
BSON_APPEND_DATE_TIME(document,
"birthday", mktime(&person->birthday) *
1000);
bson_append_array_begin(document,
"skill", -
1, &child);
iArySize =
sizeof(person->skillAry) /
sizeof(
char*);
for (i =
0; i < iArySize; i++) {
if (NULL == person->skillAry[i]) {
break;
}
keylen = bson_uint32_to_string(i +
1, &key, buf,
sizeof buf);
bson_append_utf8(&child, key, (
int) keylen, person->skillAry[i], -
1);
}
bson_append_array_end(document, &child);
bson_append_array_begin(document,
"degrees", -
1, &child);
iArySize =
sizeof(person->degreeAry) /
sizeof(TAG_DEGRESS);
for (i =
0; i < iArySize; i++) {
if ((NULL == person->degreeAry[i].degree) || (NULL == person->degreeAry[i].school)) {
break;
}
keylen = bson_uint32_to_string(i, &key, buf,
sizeof buf);
bson_append_document_begin(&child, key, keylen, &child2);
bson_append_utf8(&child2,
"school", -
1, person->degreeAry[i].school, -
1);
bson_append_utf8(&child2,
"degree", -
1, person->degreeAry[i].degree, -
1);
bson_append_document_end(&child, &child2);
}
bson_append_array_end(document, &child);
}
#include "MongoDbEx.h"
BOOL DbOptInit()
{
mongoc_init();
}
BOOL DbOptUnInit()
{
mongoc_cleanup();
}
mongoc_client_t* DbOptOpenConect(
const char* pConnectUrl)
{
mongoc_client_t* pDbClient =
NULL;
if (
NULL != pConnectUrl) {
pDbClient = mongoc_client_new(pConnectUrl);
}
return pDbClient;
}
void DbOptCloseConect(mongoc_client_t*& pDbConnect)
{
if (
NULL != pDbConnect) {
mongoc_client_destroy(pDbConnect);
pDbConnect =
NULL;
}
}
mongoc_database_t* DbOptOpenDb(mongoc_client_t* pDbConnect,
const char* pDbName)
{
mongoc_database_t* pDb =
NULL;
if (
NULL != pDbConnect) {
pDb = mongoc_client_get_database(pDbConnect, pDbName);
}
return pDb;
}
void DbOptCloseDb(mongoc_database_t*& pDb)
{
if (
NULL != pDb) {
mongoc_database_destroy(pDb);
pDb =
NULL;
}
}
mongoc_collection_t* DbOptOpenTbl(mongoc_database_t* pDb,
const char* pTblName)
{
mongoc_collection_t* pTbl =
NULL;
pTbl = mongoc_database_get_collection(pDb, pTblName);
return pTbl;
}
void DbOptCloseTbl(mongoc_collection_t*& pTbl)
{
if (
NULL != pTbl) {
mongoc_collection_destroy(pTbl);
pTbl =
NULL;
}
}
bool DbOptExec_command_simple(
mongoc_client_t* pClient,
const char* pcDbName,
const char* pcCommand,
bson_error_t* pError)
{
bool retval =
false;
bson_t* command =
NULL;
bson_t reply;
do {
command = BCON_NEW(pcCommand, BCON_INT32(
1));
retval = mongoc_client_command_simple(pClient, pcDbName, command,
NULL, &reply, pError);
if (!retval) {
break;
}
else {
ShowReply(pcCommand, &reply);
}
}
while (
0);
bson_destroy(&reply);
bson_destroy(command);
return retval;
}
bool DbOptExec_command_simple(
mongoc_client_t* pClient,
const char* pcDbName,
const char* pcCommand,
const char* pcValue,
bson_error_t* pError)
{
bool retval =
false;
bson_t reply;
bson_t* doc =
NULL;
char* sz_cmd =
NULL;
do {
sz_cmd = bson_strdup_printf(
"{'%s': '%s'}", pcCommand, pcValue);
doc = new_formatv_bson(
"%s", sz_cmd);
retval = mongoc_client_command_simple(pClient, pcDbName, doc,
NULL, &reply, pError);
bson_free(sz_cmd);
if (!retval) {
break;
}
else {
ShowReply(pcCommand, &reply);
}
}
while (
0);
bson_destroy(&reply);
bson_destroy(doc);
return retval;
}
bool DbOptAddDocument(mongoc_collection_t*& pTbl, bson_t* document, bson_error_t* error)
{
bool bRc =
false;
do {
if ((
NULL == pTbl) || (
NULL == document) || (
NULL == error)) {
break;
}
bRc = mongoc_collection_insert(pTbl, MONGOC_INSERT_NONE, document,
NULL, error);
}
while (
0);
return bRc;
}
void ShowErrMsg(
const char* pTip, bson_error_t* error)
{
if (
NULL != error) {
fprintf(stderr,
"[%s] error : %s\n\n", (
NULL != pTip) ? pTip :
"", error->message);
}
}
void ShowReply(
const char* pTip, bson_t* reply)
{
char* str =
NULL;
if (
NULL != reply) {
str = bson_as_json(reply,
NULL);
if (
NULL != str) {
printf(
"[%s] %s\n\n", (
NULL != pTip) ? pTip :
"", str);
bson_free(str);
}
}
}
void ShowDocument(
const char* pTip, bson_t* document)
{
char* str =
NULL;
if (
NULL != document) {
str = bson_as_json(document,
NULL);
if (
NULL != str) {
printf(
"[%s] %s\n\n", (
NULL != pTip) ? pTip :
"", str);
bson_free(str);
}
}
}
bson_t* new_formatv_bson(
const char* json, ...)
{
va_list args;
bson_error_t error;
char* formatted;
char* double_quoted;
bson_t* doc;
if (json) {
va_start(args, json);
formatted = bson_strdupv_printf(json, args);
va_end(args);
double_quoted = single_quotes_to_double(formatted);
doc = bson_new_from_json((
const uint8_t*) double_quoted, -
1, &error);
if (!doc) {
fprintf(stderr,
"%s\n", error
.message);
abort();
}
bson_free(formatted);
bson_free(double_quoted);
}
else {
doc = bson_new();
}
return doc;
}
char* single_quotes_to_double(
const char* str)
{
char* result = bson_strdup(str);
char* p;
for (p = result; *p; p++) {
if (*p ==
'\'') {
*p =
'"';
}
}
return result;
}
void show_tm(
const char* pTip,
struct tm* ptm)
{
char* pcDispTime =
NULL;
char szBuf[
260] = {
'\0'};
if (
NULL != ptm) {
strftime(szBuf,
260 ,
"%Y-%m-%d %H:%M:%S", ptm);
printf(
"[%s] time is %s\n", (
NULL != pTip) ? pTip :
"", szBuf);
}
}
void setTime(
struct tm* ptm,
int year,
int month,
int day,
int hour,
int min,
int sec)
{
time_t _time_t1;
struct tm* ptmUtc =
NULL;
struct tm* ptmLocal =
NULL;
if (
NULL != ptm) {
memset(ptm,
0,
sizeof(
struct tm));
ptm->tm_year = year -
1900;
ptm->tm_mon = month -
1;
ptm->tm_mday = day;
ptm->tm_hour = hour;
ptm->tm_min = min;
ptm->tm_sec = sec;
show_tm(
"org time", ptm);
_time_t1 = mktime(ptm);
ptmLocal = localtime(&_time_t1);
show_tm(
"local time", ptmLocal);
ptmUtc = gmtime(&_time_t1);
show_tm(
"utc time", ptmUtc);
memcpy(ptm, ptmLocal,
sizeof(
struct tm));
}
}
# testcase by lostspeed
# first build do : chmod 777 *
# build cmd is : ./Makefile
clear
rm ./testcase
rm ./*.o
# MongoDbEx.cpp
g++ -c MongoDbEx.cpp -o MongoDbEx.o \
-
I/usr/local/mongo_c162_driver/include/libmongoc-1.0 \
-
I/usr/local/mongo_c162_driver/include/libbson-1.0 \
-
L/usr/local/lib \
-
L/usr/local/mongo_c162_driver/lib \
-
lpthread \
-
lmongoc-1.0 \
-
lbson-1.0
# BusinessLogic.cpp
g++ -c BusinessLogic.cpp -o BusinessLogic.o \
-
I/usr/local/mongo_c162_driver/include/libmongoc-1.0 \
-
I/usr/local/mongo_c162_driver/include/libbson-1.0 \
-
L/usr/local/lib \
-
L/usr/local/mongo_c162_driver/lib \
-
lpthread \
-
lmongoc-1.0 \
-
lbson-1.0
# testcase_main.cpp
g++ testcase_main.cpp BusinessLogic.o MongoDbEx.o -o testcase \
-
I/usr/local/mongo_c162_driver/include/libmongoc-1.0 \
-
I/usr/local/mongo_c162_driver/include/libbson-1.0 \
-
L/usr/local/lib \
-
L/usr/local/mongo_c162_driver/lib \
-
lpthread \
-
lmongoc-1.0 \
-
lbson-1.0
ls
echo run
./testcase