mongoc_collection_count

前言

按照mongoc说明书, 做个mongoc_collection_count试验.
返回类型PRId64很大啊, 例子中格式化成字符串不好使, 只能先按longlong用了.

记录

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();
    // find db record set name : {first:x, last:cn}
    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 {
        // PRId64不好使啊, 是不是版本高才好使啊?
        // 就按照long long 使用吧
        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;
}

你可能感兴趣的:(MongoDB)