简体   繁体   English

mongo-cxx-driver-新的C ++ 11驱动程序-如何从二进制数据创建文档

[英]mongo-cxx-driver -New C++11 driver - How to create Document from binary data

for a project I need a c++ implementation for bson. 对于一个项目,我需要bson的c ++实现。 According to the BSON Specs there are serveral bson c++ implementations. 根据BSON规范,有服务器bson c ++实现。 I already tried each of them. 我已经尝试了每个。 Bson-cpp isnt supported anymore. 不再支持Bson-cpp Bson-cxx wont even compile on my debian stretch. Bson-cxx甚至不会在我的debian上编译。

So I tried the official mongodb-cxx-driver. 所以我尝试了官方的mongodb-cxx-driver。 But i want to use the current master-branch of the driver which contains the new c++11 driver because it uses cmake as build system. 但是我想使用包含新的c ++ 11驱动程序的驱动程序的当前主分支,因为它使用cmake作为构建系统。 I already managed to create a bson document and convert it to a binary array. 我已经设法创建一个bson文档并将其转换为二进制数组。 My problem at the moment is that I don't get how to create a bson document from an array of binary data even after digging through their code for two hours. 我目前的问题是,即使在他们的代码中挖掘了两个小时之后,我仍然无法从二进制数据数组中创建bson文档。

Is this functionality even implemented at the moment ? 此功能目前是否已实现?

The C++11 driver from MongoDB offers a few interfaces for BSON writing and one interface for reading. MongoDB的C ++ 11驱动程序提供了一些用于BSON写入的接口和一个用于读取的接口。

If what you want to do is read some already serialized bson: 如果您要读取的是已经序列化的bson,请执行以下操作:

bsoncxx::document::view view(data, length); // make a view
view["key"]; // fetch a key
view.find("key"); // map style iterator
for (auto x : view) {} // C++11 for loop

// or to load an array
bsoncxx::array::view view(data, length);
view[N];
// etc...

If you'd like to write some bson: 如果您想写一些bson:

For a streaming api: 对于流式API:

using namespace bsoncxx::builder::stream;
// Use builder::stream::array for array building
document b;

// Makes { "key" : "value", "subdoc" : { "sub key a" : 1 } }
b << "key" << "value" << "subdoc" <<
  << open_document << "sub key a" << 1 << close_document;

// to concatenate
b << concatenate(view);

// to use a non-deducible type like a sub doc
b << "key" << bsoncxx::types::b_document{view}

b.view() // gets a view

// take ownership of the bson bytes in a move only value type
bsoncxx::document::value v = b.extract()

For a simple key value api: 对于简单的键值api:

using namespace bsoncxx::builder::basic;

// Use builder::basic::array for array building
document b;

b.append(
    kvp("key", "value"),
    // Use sub_array for sub arrays
    kvp("subdoc", [](sub_document sd) {
        sd.append(kvp("sub key a", 1));
    })
);

That core api you found is certainly functional, but it's meant to provide a low level machine for BSON appending over a reallocing buffer, rather than an easy to use client interface. 您发现的那个核心api确实可以正常工作,但是它的目的是为BSON提供一个低级机器,以在重新分配缓冲区上附加内容,而不是易于使用的客户端接口。 Prefer the builder::basic or builder::stream apis if you'd like some template magic to make construction more declarative. 如果您希望使用一些模板魔术来使构造更具说明性,请选择builder :: basic或builder :: stream api。

After some more digging in their code I found the answer: 在深入研究他们的代码后,我找到了答案:

You can use: 您可以使用:

bsoncxx::document::view(uint8_t* data, size_t length)

in order to create new view 为了创建新的视图

Then you can use 那你可以用

bsoncxx::builder::core(bool isArray)

to create a new core .On this core you can call 创建一个新的核心。在这个核心上,您可以调用

core.concatenate(view)

to insert the view into the new core. 将视图插入新的核心。 Afterwards you can create new sub_document with: 之后,您可以使用以下命令创建新的sub_document:

bsoncxx::builder::basic:sub_document(core* c)

sub_document should provide all methods needed to extract the fields from the bson message. sub_document应该提供从bson消息中提取字段所需的所有方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM