如何使用mongocxx c ++驱动程序递归生成Mongodb文档? [英] How to generate Mongodb documents recursively using mongocxx c++ driver?
问题描述
如何使用mongocxx c ++驱动程序递归生成Mongodb文档? 1.我使用mongocxx c ++驱动程序v.3和c ++ 11. 2.这是我的main.cpp方法,该方法解析十六进制字符串并生成mongocxx代码,如下所示: 控制台:$ ./主要剖析0x160301012c01000128030340c70e243001b96d8c 和输出:
How to generate Mongodb documents recursively using mongocxx c++ driver? 1. I use mongocxx c++ driver v.3 and c++11. 2. Here is my main.cpp method, which parses hex string and generate mongocxx code like this: console:$ ./main dissect 0x160301012c01000128030340c70e243001b96d8c and the output:
<< "MainType" << bsoncxx::builder::stream::open_document
<< "TLSRecord" << bsoncxx::builder::stream::open_document
<< "type"<< "16"
<< "version"<< "0301"
<< "length"<< "012C"
<< "hsMsg" << bsoncxx::builder::stream::open_document
<< "type"<< "01"
<< "length"<< "000128"
<< "clientHello" << bsoncxx::builder::stream::open_document
<< "version"<< "0303"
<< "random"<< "40C70E243001B96D8C"
<< "session_id_length"<< ""
<< bsoncxx::builder::stream::close_document
<< bsoncxx::builder::stream::close_document
<< bsoncxx::builder::stream::close_document
-
此后,我需要将其推入mongodb中.
After this I needed to push it in mongodb.
-
从解剖方法形式main.cpp调用连接方法 十六进制字符串解析后调用mongodb连接
Call connection method from dissect method form main.cpp Call mongodb connection after hex string parsing
创建mongodbConnection:
Create the mongodbConnection:
调用MongodbMapper将生成的代码映射到db
Call MongodbMapper to map generated code to db
调用GenerateDocument自动生成 连接-> Mapp->生成->插入
Call GenerateDocument to generate it automatically Connect -> Mapp -> Generate -> Insert
在这里,我堆积如山,尝试编译时出现错误.
And Here I got stacked, got error when try to compile it.
src/MongodbMapper.cpp:76:6: note: candidate function not viable: no known conversion from
'bsoncxx::v_noabi::builder::stream::key_context<bsoncxx::v_noabi::builder::stream::key_context<bsoncxx::v_noabi::builder::stream::closed_context> >' to
'bsoncxx::builder::stream::document &' for 3rd argument
void generateDocument(DataUnit& node, int level, bsoncxx::builder::stream::document& doc) {
src/MongodbMapper.cpp:76:6: note: candidate function not viable: no known conversion from
'bsoncxx::v_noabi::builder::stream::key_context<bsoncxx::v_noabi::builder::stream::key_context<bsoncxx::v_noabi::builder::stream::closed_context> >' to
'bsoncxx::builder::stream::document &' for 3rd argument
void generateDocument(DataUnit& node, int level, bsoncxx::builder::stream::document& doc) {
推荐答案
很难确定没有看到您发布的细分受众群的上下文,但是看起来您遇到的问题与流构建器上的<<
运算符.实际上,流生成器的名称不正确;它不是典型的C ++单词意义上的流",因为<<
运算符的输出类型有时会与左侧操作数不同.特别是,无论何时使用open_document
或close_document
之类的表达式,表达式输出的类型都会与左侧操作数不同.因此,通常需要存储这些表达式之一的输出.
It's hard to be sure without seeing the context of the segment you posted, but it looks like the issue you're running into is with the output type of the <<
operator on the stream builder. The stream builder is actually misnamed; it isn't a "stream" in the typical C++ sense of the word, as the output type of the <<
operator will sometime be different than the left-hand-side operand. In particular, whenever you use something like open_document
or close_document
, the type that expression outputs will be different than what the left-hand-side operand is. Because of this, you'll generally need to store the output of one of those expressions.
由于在这种情况下流构建器经常引起混乱,因此通常最好改用基本构建器.基本构建器的语法稍微有些冗长,但要犯一个细微的错误要困难得多,而且当您犯错时,编译器错误消息也更容易理解.
Because of the confusion that the stream builder often causes in cases like this, it's generally preferable to use the basic builder instead. While the basic builder's syntax is a little more verbose, it's much harder to make a subtle mistake with it, and when you do make a mistake, the compiler error messages are much easier to understand.
以下是使用基本构建器构建相同文档的方法:
Here's how you would build up the same document with the basic builder:
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/builder/basic/sub_document.hpp>
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::sub_document;
bsoncxx::builder::basic::document doc;
// Build the document
doc.append(kvp("MainType", [](sub_document sub_doc1) {
sub_doc1.append(kvp("TLSRecord", [](sub_document sub_doc2) {
sub_doc2.append(kvp("type", "16"),
kvp("version", "0301"),
kvp("length", "012C"),
kvp("hsMsg", [](sub_document sub_doc3) {
sub_doc3.append(kvp("type", "01"),
kvp("length", "000128"),
kvp("clientHello", [](sub_document sub_doc4) {
sub_doc4.append(
kvp("version", "0303"),
kvp("random", "40C70E243001B96D8C"),
kvp("session_id_length", ""));
}));
}));
}));
}));
// Get a view of the document being built and do something with it.
do_something_with_document_view(doc.view());
// Extract the document from the builder and do something with it.
do_something_with_owned_document(doc.extract());
bsoncxx::builder::basic::document::append
采用任意数量的kvp
(键-值对)并将其附加到构建器.对于诸如字符串之类的基本类型,您可以仅将值作为第二个参数传递.要构建子文档,请使用lambda作为第二个参数,该参数采用bsoncxx::builder::basic::sub_document
,然后以相同的方式附加到该子文档构建器.
bsoncxx::builder::basic::document::append
takes a arbitrary number of kvp
's (key-value pairs) and appends them to the builder. For basic types like strings, you can just pass the value as the second argument. To build up a subdocument, use a lambda as the second argument that takes a bsoncxx::builder::basic::sub_document
and then append to that subdocument builder in same the same way.
要从构建器中获取文档,可以使用view()
或extract()
方法. view()
返回bsoncxx::document::view()
,这是文档的未拥有视图;构建者需要在使用视图的整个过程中保持活动状态. extract()
返回bsoncxx :: document :: value,它是一个拥有的值;调用extract()
时,构建器将重置为空状态.
To get the document out from the builder, you can either use the view()
or extract()
methods. view()
returns a bsoncxx::document::view()
, which is an unowned view of the document; the builder needs to remain alive for the entire time the view is used. extract()
returns a bsoncxx::document::value, which is an owned value; when extract()
is called, the builder is reset back to the empty state.
这篇关于如何使用mongocxx c ++驱动程序递归生成Mongodb文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!