- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
MongoDB C++ driver允许创建BSON对象的两种方式(among others)。
基于流:
auto builder = bsoncxx::builder::stream::document{};
bsoncxx::document::value doc_value = builder
<< "name" << "MongoDB"
<< "type" << "database"
<< "count" << 1
<< "versions" << bsoncxx::builder::stream::open_array
<< "v3.2" << "v3.0" << "v2.6"
<< close_array
<< "info" << bsoncxx::builder::stream::open_document
<< "x" << 203
<< "y" << 102
<< bsoncxx::builder::stream::close_document
<< bsoncxx::builder::stream::finalize;
std::string doc = "{ "
"\"name\" : \"MongoDB\","
"\"type\" : \"database\","
"\"count\" : 1,"
"\"versions\": [ \"v3.2\", \"v3.0\", \"v2.6\" ],"
"\"info\" : {"
"\"x\" : 203,"
"\"y\" : 102"
"}"
"}";
bsoncxx::document::value bsoncxx::from_json(doc);
最佳答案
最后,我做了一些基准测试。我正在分享我的结果,以防它们对其他人有用。驱动程序版本为3.4.0。
这是基于流的版本:
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char**) {
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
for (unsigned int ix = 0; ix < 10000000 ; ++ix) {
auto builder = bsoncxx::builder::stream::document{};
bsoncxx::document::value doc_value = builder
<< "name" << "MongoDB"
<< "type" << "database"
<< "count" << 1
<< "versions" << bsoncxx::builder::stream::open_array
<< "v3.2" << "v3.0" << "v2.6"
<< bsoncxx::builder::stream::close_array
<< "info" << bsoncxx::builder::stream::open_document
<< "x" << 203
<< "y" << 102
<< bsoncxx::builder::stream::close_document
<< bsoncxx::builder::stream::finalize;
}
}
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char**) {
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
for (unsigned int ix = 0; ix < 10000000 ; ++ix) {
std::string doc = "{ "
"\"name\" : \"MongoDB\","
"\"type\" : \"database\","
"\"count\" : 1,"
"\"versions\": [ \"v3.2\", \"v3.0\", \"v2.6\" ],"
"\"info\" : {"
"\"x\" : 203,"
"\"y\" : 102"
"}"
"}";
bsoncxx::document::value doc_value = bsoncxx::from_json(doc);
}
}
c++ --std=c++11 test-stream.cpp -o test-stream $(pkg-config --cflags --libs libmongocxx)
c++ --std=c++11 test-textparsing.cpp -o test-textparsing $(pkg-config --cflags --libs libmongocxx)
$ time ./test-stream ; time ./test-stream ; time ./test-stream
real 0m16,454s
user 0m16,200s
sys 0m0,084s
real 0m17,034s
user 0m16,900s
sys 0m0,012s
real 0m18,812s
user 0m18,708s
sys 0m0,036s
$ time ./test-textparsing ; time ./test-textparsing ; time ./test-textparsing
real 0m53,678s
user 0m53,576s
sys 0m0,024s
real 1m0,203s
user 0m59,788s
sys 0m0,116s
real 0m57,259s
user 0m56,824s
sys 0m0,200s
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
using bsoncxx::builder::basic::kvp;
int main(int, char**) {
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
for (unsigned int ix = 0; ix < 10000000 ; ++ix) {
bsoncxx::builder::basic::document basic_builder{};
basic_builder.append(kvp("name", "MongoDB"));
basic_builder.append(kvp("type", "database"));
basic_builder.append(kvp("count", 1));
bsoncxx::builder::basic::array array_builder{};
array_builder.append("v3.2");
array_builder.append("v3.0");
array_builder.append("v2.6");
basic_builder.append(kvp("versions", array_builder.extract()));
bsoncxx::builder::basic::document object_builder{};
object_builder.append(kvp("x", 203));
object_builder.append(kvp("y", 102));
basic_builder.append(kvp("info", object_builder.extract()));
bsoncxx::document::value doc_value = basic_builder.extract();
}
}
c++ --std=c++11 test-basic.cpp -o test-basic $(pkg-config --cflags --libs libmongocxx)
basic
-----
real 0m20,725s
user 0m20,656s
sys 0m0,004s
real 0m20,651s
user 0m20,620s
sys 0m0,008s
real 0m20,102s
user 0m20,088s
sys 0m0,000s
stream
------
real 0m11,841s
user 0m11,780s
sys 0m0,024s
real 0m11,967s
user 0m11,932s
sys 0m0,008s
real 0m11,634s
user 0m11,616s
sys 0m0,008s
textparsing
-----------
real 0m37,209s
user 0m37,184s
sys 0m0,004s
real 0m36,336s
user 0m36,208s
sys 0m0,028s
real 0m35,840s
user 0m35,648s
sys 0m0,048s
test-basic.cpp
代码令人担忧?还是结果有意义?
关于c++ - MongoC++驱动程序BSON的构建:基于流与基于字符串解析。哪一个性能更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55385691/
我试图理解 BSON 符号 来自网站 BSON Site .但是,我无法理解相关性背后的原因。 我也提到了以下问题,但由于以下原因,我不相信。 Question 1 : 不熟悉 ruby 实现 Que
我收到以下错误: { [Error: Cannot find module '../build/Release/bson'] code: 'MODULE_NOT_FOUND' } js-bson:
假设我们有一个字符串,里面有二进制 bson 数据。如何加载到 bson object ? 最佳答案 我想在同一个页面上有一个例子可以做到这一点: BSONObjBuilder b; b << "na
我正在寻找如下所示的 type_of 方法: import bson bson.type_of(42) # it should return "int". bson.type_of("hello")
Base64 编码的 BSON 比 BSON 小吗? 最佳答案 Piskvor 的权利,base64 编码的任何东西都比原始长。您对某些内容进行 base64 编码以使其进入具有有限字符轨道的 cha
目前正在做一个 Golang 项目,但我得到了一些 Controller package controller import ( "go.mongodb.org/mongo-driver
这个特定的问题与使用 mongodb 和 golang 包 mongo-driver 相关。 ,但我认为这适用于与 mongodb 的大多数接口(interface)。 使用 Find 时要从集合中查
目前正在做一个 Golang 项目,但我得到了一些 Controller package controller import ( "go.mongodb.org/mongo-driver
可能相关:How to use interface type as a model in mgo (Go)? 我有一个像这样的结构: type Game struct { ID b
有没有一种方法可以使用 MongoDB C++ 驱动程序中的 BSON() 宏来生成 BSON 将空值。例如,为了生成一个 BSON 来表示这个文档:{"a": "foo", "b": null}:
我通过以下两种不同的方法生成了一个 ObjectId: user@ubuntu:~$ python Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
我如何转换 BsonDocument到 FilterDefinition实例? 它是新的 MongoDb C# 驱动程序提供的类。 最佳答案 BsonDocument 和 FilterDefiniti
我正在尝试从 Json 生成 Bson。我尝试使用 Json.Net,但似乎有记录的行为,其中库为整数字段生成 uint64。不幸的是,我们必须使用 uint32。 因此我正在尝试使用 mongodb
我正在使用 https://github.com/mongodb/mongo-go-driver和目前正在尝试实现此类结构的部分更新 type NoteUpdate struct { ID
我目前正在尝试读取 bson 文件以将其导入数据库。我已经可以读取该文件并将其作为字节打印,但最终只收到 bson.errors.InvalidBSON: objsize Too Large 错误。
我尝试在 arch linux 迷你计算机(cubox)上安装 mean.io 堆栈。所以我安装了 nodejs 和 mongodb 包。 我用 Git 检索了堆栈,进行了 npm 安装(没问题)但是
为什么会输出false?我期待 true... package main import ( "fmt" "time" "gopkg.in/mgo.v2/bson" )
这是一种愚蠢的语法错误,尝试了很多方法,但都无法正常工作,请大家帮忙。 使用 mgo 在 Go 中使用 MongoDB,我只是尝试简化 $ne 运算符的使用,代码如下所示,但不断出现编译语法错误: l
我设置了新的 Typescript/React 项目,在 tsconfig.json 文件中收到此错误消息 "找不到 'bson' 的类型定义文件。该文件在程序中,因为:隐式类型库 'bson' 的入
我尝试将数据从 SQL Server 迁移到 MongoDB,但在将数据导入到 MongoDB 的最后阶段遇到了以下类型错误。 mongoImp = dbo.insert_many(jArray)
我是一名优秀的程序员,十分优秀!