免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4750 | 回复: 0
打印 上一主题 下一主题

[MongoDB] c 语言驱动访问mongodb 一些问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2016-11-13 20:36 |只看该作者 |倒序浏览
本帖最后由 xpston008 于 2016-11-13 20:38 编辑

c驱动  mongo-c-driver-1.4.2(功能:一个线程查询数据后更新一个状态为处理状态
一个线程将处理结果后一些状态更新到这个表
数据量很大,所以分开两个线程。
  1. BOOL RSmppMongoDb::_db_res_query()
  2. {
  3.    const bson_t *doc;
  4.    bson_error_t error;

  5.    const char *collection_name = "cac_sms_sent_pool";
  6.    bson_t *query;
  7.    char *str;
  8.    mongoc_collection_t *collection;
  9.    mongoc_cursor_t *cursor;


  10.    query = bson_new ();  
  11.    BSON_APPEND_UTF8 (query, "apply_state", "0");
  12.    collection = mongoc_client_get_collection (m_pClient, "cac", collection_name);
  13.    cursor     = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, \
  14.                                         query, NULL,/* Fields, NULL for all. */ NULL); /* Read Prefs, NULL for default */
  15.    while (mongoc_cursor_next (cursor, &doc))
  16.    {
  17.        str = bson_as_json (doc, NULL);
  18.        fprintf (stdout, "%s\n", str);
  19.        bson_free (str);
  20.    }

  21.    if (mongoc_cursor_error (cursor, &error)) {
  22.        fprintf (stderr, "Cursor Failure: %s\n", error.message);
  23.        return EXIT_FAILURE;
  24.    }

  25.    bson_destroy (query);
  26.    mongoc_cursor_destroy (cursor);
  27.    mongoc_collection_destroy (collection);
  28. }
复制代码
1.  这里面有哪些是非线程安全的函数。 是不是所有的操作都需要加锁,保证线程安全。2. 如此复杂的语句在c 里面该如何写 ,下面是mysql的语句。
  1. "UPDATE tb_test  set apply_time=str_to_date('%s','%s') , \
  2.                       apply_state=CASE WHEN apply_state='-1' THEN '-1' ELSE '%d' END , \
  3.                       fail_apply_reason=concat(IFNULL(fail_apply_reason,''),'%s')    , \
  4.                       return_message_id =concat(IFNULL(return_message_id,''),'%s ')    \
  5.                       WHERE message_id='%s' ",iter->apply_time,"%Y%m%d%H%i%s",         \
  6.                 iter->last_appy_state,iter->fail_reason,iter->return_message_id,iter->rec_id.c_str());
复制代码

大神们帮忙回复。






您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP