您的位置:首页 > 数据库 > Mongodb

MongoDB&C++开发(五)C++ Driver API 主要类及成员函数

2017-10-01 20:37 726 查看
MongoDB C++ Driver API 官方链接

1. bsoncxx

bsoncxx::stream



这个命名空间下有一些模板类,模板类中有一些重载<<运算符的函数方法。常用的array和document继承了这些类。

key_context

即键值对中的键key

single_context

一个单独的值,调用single_c
4000
ontext往value_context或array_context中写值

value_context

即键值对中的一个值,后面可以跟很多键值对

在document中,第一个参数是key_context,然后是value_context,然后是key_context

builder << key_context << value_context <

array_context

任意多个值

builder << array_context << array_context <

array

构造一个BSON array 的流式接口,继承了array_context。



array() 构造函数

view() BSON array的一个视图

extract() 将这个array的所有权转移给调用者

clear() 将array重置为空

document

与array类似,也有这样四个public的成员函数。

bsoncxx::array & bsoncxx::document

这两个命名空间中都有三个类 element,value,view

以document中的为例



element:就理解成为一个document中的类型不定的元素(可能是key,可能是值),调用type()能得到这个element的类型,调用key()可以得到键(stdx::string_view key () const 注意这个函数的返回值),使用形如get_X()的函数(如types::b_document get_document () const,types::b_array get_array () const,types::b_double get_double () const等)可以得到特定类型的值。

value:是一个只读的有自己的缓冲区的document。(应慎重使用,尽量使用view)

view:是一个只读的,没有所有权的document的视图。

2. mongocxx

mongocxx::collection类

这个类应该是最重要的吧~

函数参数中filter是筛选条件是一个bsoncxx::document::view_or_value,最后一个参数是一个操作可选的参数。

count

std::int64_t count (bsoncxx::document::view_or_value filter, const options::count &options=options::count())

计算符合filter条件的documents数目。

delete_many & delete_one

stdx::optional< result::delete_result > delete_many (bsoncxx::document::view_or_value filter, const options::delete_options &options=options::delete_options())

删除符合filter条件的所有文件。

stdx::optional< result::delete_result > delete_one (bsoncxx::document::view_or_value filter, const options::delete_options &options=options::delete_options())

删除符合filter条件的一个文件。

drop

void drop ()

从数据库中删除此collection及其所包含的所有documents。

find & find_one & find_one_and_delete……



replacement和update是需要替换/更新的内容。

insert_one & insert_many



replace_one & update_one & update_many



replacement和update是需要替换/更新的内容。

mongocxx::model



delete_many & delete_one

delete_many 是删除所有符合条件的documents,delete_one是删除第一个符合条件的document。(注意:第一个~)

delete_many (bsoncxx::document::view_or_value filter)

delete_one (bsoncxx::document::view_or_value filter)

注意参数

insert_one

插入一个document

insert_one (bsoncxx::document::view_or_value document)

replace_one

替换符合filter条件的一个document,其中的内容替换成replacement

replace_one (bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value replacement)

update_many & update_one

update_many是更新所有符合filter条件的documents,update是更新的内容。

update_one是更新一个符合filter条件的document,update是更新的内容。

update_many (bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value update)

update_one (bsoncxx::document::view_or_value filter, bsoncxx::document::view_or_value update)

mongocxx::result



这个命名空间里面封装的类主要用于返回某种操作的批处理结果,以及进行一些简单的操作文件个数的统计。

bulk_write

这个类里面有一些形如 ××operation_count()的函数,得到经过××操作之后插入/匹配/修改/删除/……的文件的个数。

其他的几个类

都有const result::bulk_write & result () const这样的一个函数,返回进行某种操作之后的批处理结果。也有一些形如 ××operation_count()的函数。

mongocxx::options



这个命名空间里面封装的类主要用于设置MongoDB的一些操作的可选参数。

如:aggregate设置MongoDB 统计操作的一些可选参数的类。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mongodb c++ c++-driver