序
今天同事让我帮测试一下mongo库的响应. 写了一个query, 按照列的与关系去查结果集. 这个最普通了 然后, 我想到如果查多列条件是或的关系, 该怎么玩啊? 做了实验进行了验证.
实验
int testcase_mongoc_example_document_query_multi_cols_and(
int argc,
char* argv[]);
int testcase_mongoc_example_document_query_multi_cols_or(
int argc,
char* argv[]);
printf("============================================================\n");
printf(">> testcase build time 2017-5-5 11:14\n");
printf("============================================================\n");
testcase_mongoc_example_document_query_multi_cols_and(argc, argv);
testcase_mongoc_example_document_query_multi_cols_or(argc, argv);
#define MONGOSRV_IP "192.168.2.60" // mongo server ip
#define MONGOSRV_PORT "20001" // mongo server port
#define MONGOAPP_NAME "query_multi_cols" // app name for trace
#define MONGODB_NAME "dbname1" // database name
#define MONGOTBL_NAME "tblname1" // table name
#define MONGOTBL_COL1_NAME "tbl_col1" // column 1 name
#define MONGOTBL_COL2_NAME "tbl_col2" // column 2 name
#define MONGOTBL_COL3_NAME_TO_OR "tbl_col3" // column 2 name to query by or
int testcase_mongoc_example_document_query_multi_cols_and(
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'};
printf(
"testcase_mongoc_example_document_query_multi_cols_and : 2017/5/5 13:36\n");
mongoc_init();
client = mongoc_client_new(
"mongodb://" MONGOSRV_IP
":" MONGOSRV_PORT
"/?appname=" MONGOAPP_NAME);
collection = mongoc_client_get_collection(client, MONGODB_NAME, MONGOTBL_NAME);
query = BCON_NEW(MONGOTBL_COL1_NAME, BCON_INT64(
1493931600000), MONGOTBL_COL2_NAME, BCON_INT64(
2685446336));
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_query_multi_cols_or(
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'};
printf(
"testcase_mongoc_example_document_query_multi_cols_or : 2017/5/5 13:51\n");
mongoc_init();
client = mongoc_client_new(
"mongodb://" MONGOSRV_IP
":" MONGOSRV_PORT
"/?appname=" MONGOAPP_NAME);
collection = mongoc_client_get_collection(client, MONGODB_NAME, MONGOTBL_NAME);
query = BCON_NEW(
"$or",
"[",
"{", MONGOTBL_COL1_NAME, BCON_INT64(
1493931600000),
"}",
"{", MONGOTBL_COL3_NAME_TO_OR, BCON_INT64(
284509),
"}",
"]");
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;
}
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);
}
}
}