c++ - Mongo - 从值中获取 view_or_value?

标签 c++ mongodb mongo-cxx-driver

我刚刚开始使用 Mongo,在将文档写入集合时遇到了一些问题。我似乎无法从 document::value 转换为 string::view_or_value。关于整理这些类型的任何提示?我尝试直接发送 doc_value,但这对插入的无效。

#include "stdafx.h"
#include <cstdint>
#include <iostream>
#include <vector>
#include <mongocxx/instance.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>

using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;

int main()
{
    mongocxx::instance instance{};
    mongocxx::client client{ mongocxx::uri{} };
    mongocxx::database db = client["mydb"];
    bsoncxx::builder::stream::document builder{};
    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;

    db.collection("cats").insert_one(bsoncxx::string::view_or_value(doc_value));
    return 0;
}

最佳答案

mongocxx::collection::insert_one 采用 bsoncxx::document::view_or_value,而不是 bsoncxx::string::view_or_value .我希望以下内容能够正常工作:

db.collection("cats").insert_one(std::move(doc_value));

请注意,这会将文档作为值进行传输。如果你只想传递一个 View :

db.collection("cats").insert_one(doc_value.view());

不会转移所有权。

关于c++ - Mongo - 从值中获取 view_or_value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45170901/

相关文章:

c++ - 未找到 SDL2_image

c++ - C++ 中的字符串操作和文件 IO?

regex - 在Rust中使用正则表达式查询MongoDB

mongodb - 使用MongoDB投影

c++ - MongoDB C++ 教程程序失败 : 'mongocxx::v_noabi::logic_error'

c++ - 加载共享库时出错 : libbsoncxx. so._noabi:无法打开共享对象文件:没有这样的文件或目录

python - MongoDB Python 和 C++ 客户端 - 多个实例出错

c++ - 无法无损地将 rgb 转换为 YCbCr 并返回

mongodb - 如何将 Robomongo 连接到 MongoDB

c++ - 为什么 conservativeResize 不能与函数中的 Ref 变量一起使用?