【问题标题】:How to copy a bsoncxx::builder::basic::document to another?如何将 bsoncxx::builder::basic::document 复制到另一个?
【发布时间】:2017-08-02 03:32:21
【问题描述】:

有什么方法可以安全地将一个 bsoncxx 文件复制到另一个文件。 在下面的代码中我无法做到这一点

class DocClass
{
private:
    bsoncxx::builder::basic::document m_doc;
public:
    bsoncxx::builder::basic::document& copy(bsoncxx::builder::basic::document& obj)
    {
        obj = m_doc; //Not allowed
        //Error C2280   attempting to reference a deleted function
    }
};

即使复制后也不应该对对象造成任何伤害。 请帮忙。

谢谢, 世斌

【问题讨论】:

  • 这里似乎有些混乱。代码和问题写得好像你想复制一个bsoncxx::document::value,但你是在传递bsoncxx::builder::basic::document。这些是非常不同的事情。你真的是要模仿建设者吗?
  • 是的,我想复制一个构建器。 [我发现builder默认是不可复制的]还有什么办法吗?
  • 那么,让我问一个显而易见的问题。 为什么您要复制构建器?
  • 基本上我在做mongodb驱动升级。所以在客户端代码中是这样的:void GetBasicPropertiesToFetch(const wstring& collectionName, CMongoBsonBuilder &bsonFieldsToReturn) { //need to copy the class member (builder ) to bsonFieldsToReturn } CMongoBsonBuilder 是使用 BSONObjBuilder 的包装类。现在使用 bsoncxx::builder::basic::document 代替 BSONObjBuilder。

标签: mongodb c++11 bson mongo-cxx-driver


【解决方案1】:

如果你想复制一个 bsoncxx::document::value,你可以从它的视图构造一个新的:

bsoncxx::document::value foo = ...;
bsoncxx::document::value bar{foo.view()};

bsoncxx::builder::basic::document 只能移动,不能复制。但是,您可以使用 view() 方法从构建器查看基础文档,这可能会根据您的用例对您有所帮助。不过,您仍然只能从构建器中发送一次 extract,因此如果您需要多个 document::value,则必须依赖构建第二个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    相关资源
    最近更新 更多