gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:11:12 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com