mongo : query multi cols by "and" or by "or"

本文介绍了如何在MongoDB中进行多列条件查询,并通过具体示例演示了使用与(AND)及或(OR)逻辑进行查询的方法。

今天同事让我帮测试一下mongo库的响应.
写了一个query, 按照列的与关系去查结果集. 这个最普通了
然后, 我想到如果查多列条件是或的关系, 该怎么玩啊? 做了实验进行了验证.

实验

// mongo : query multi cols by "and" or by "or"
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

// select * from mytbl where col1 = 123 and col2 = 456
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 info for debug
    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);
    // select * from tbl where col1 = xxx and col2 = yyy
    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 info for debug
    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);
    // select * from tbl where col1 = xxx or col2 = yyy
    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);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值