mongo-c-driver MongoDB使用笔记

本文介绍了如何使用mongo-c-driver进行MongoDB的查询、更新操作,包括查询条件、投影、更新策略、数组字段操作以及ISODate类型字段的处理。详细讲解了不同查询操作和更新选项,如不等于、包含、大于、小于等条件,并提供了示例代码。
摘要由CSDN通过智能技术生成

最近一个项目,使用了MongoDB,c 语言调用 mongo-c-driver 函数实现数据库的操作,网上资料较少,自己摸索总结了一些方法,做个记录。

一、查询

1、函数 mongoc_collection_find_with_opts 和  mongoc_collection_find 

     如果只有查询条件,没有输出条件,这两个函数都可使用;如果有输出条件,则使用mongoc_collection_find_with_opts,结合关键字:projection

例子:

query = BCON_NEW("AfileID", FileID);
bson_init(&child3);
BSON_APPEND_DOCUMENT_BEGIN(&child3, "projection", &child);
BSON_APPEND_INT32(&child, "_id", 0);
BSON_APPEND_INT32(&child, "username", 0);
bson_append_document_end(&child3, &child);
cursor = mongoc_collection_find_with_opts(collection_file, query, &child3, NULL);

    其中,_id是MongoDB自动添加的id字段, BSON_APPEND_INT32(&child, "_id", 0);中的0,表示不输出。

2、比较

1)不等于:not eq:

                        bson_append_document_begin(&child2, "username", -1, &child3);
                        bson_append_document_begin(&child3, "$not", -1, &child4);
                        BSON_APPEND_UTF8(&child4, "$eq", m_username);
                        bson_append_document_end(&child3, &child4);
                        bson_append_document_end(&child2, &child3);

2)包含:regex (任意位置开始匹配)

                        bson_append_document_begin(&child2, "userpart", -1, &child3);
                        BSON_APPEND_UTF8(&child3, "$regex", sstr2.c_str());
                        bson_append_document_end(&child2, &child3);

                regex  ^  (从头开始匹配)

   
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值