Mongo - 从值中得到 view_or_value?

Mongo - get a view_or_value from a value?

我刚刚开始使用 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));

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

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

不会转移所有权。