Passing documents as parameters

I’m sure there’s a way, but I can’t find the proper way to populate my document in a method so I can work with it in other methods. Here’s the non-working version of what I’m trying to do.

bool DBClass::GetADoc(bsoncxx::document::value& resultdoc)
{
    bool bRtn = false;

    mongocxx::uri uri("mongodb://localhost:27017");
    mongocxx::client client(uri);
    mongocxx::database database = client["test_database"];
    mongocxx::collection collection = database["test_collection"];

    if(collection)
    {
        auto tmpDoc = collection.find_one();
        if(tmpDoc)
        {
            resultdoc = tmpDoc->view();
            bRtn = true;
        }
    }

    return bRtn;
}

Hi @Randy_Culler1 and welcome to MongoDB community forums!!

With the latest MongoDB driver version as 3.7.0, I tried the below code to view the documents of the collection.

#include <iostream>
#include <cstdint>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.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(int, char**) {
    mongocxx::instance inst{};
    mongocxx::uri uri("mongodb+srv://findThief:findThief@cluster0.sqm88.mongodb.net/?retryWrites=true&w=majority");
    std::cout << "Connecting to MongoDB Atlas ...\n";
    mongocxx::client conn{uri};
    auto collection = conn["test"]["cxxexample"];
    auto find_one_result = collection.find_one({});
    if(find_one_result) {
        std::cout << bsoncxx::to_json(*find_one_result) << "\n";
    }
    std::cout << "Completed\n";
}

and the output was successfully printed as:

Connecting to MongoDB Atlas ...
{ "_id" : { "$oid" : "64abbab0bd85fb38a501e2b4" }, "filed_value" : "ABC" }
Completed

Could you please try the above code and let us know if the above works for you. If not, could you please share the driver version along with the error message that you see while executing the above code.
For more, you can refer to the documentation for Tutorial for Mongocxx.

Regards
Aasawari

Sorry, I should have been more clear. My question is how to return a document value in a parameter in C++.

I found a solution that seems to work:

bool DBClass::GetADoc(bsoncxx::document::value& resultdoc)
{
    bool bRtn = false;

    mongocxx::uri uri("mongodb://localhost:27017");
    mongocxx::client client(uri);
    mongocxx::database database = client["test_database"];
    mongocxx::collection collection = database["test_collection"];
    
    if (collection)
    {
        auto tmpDoc = collection.find_one({});
        if (tmpDoc)
        {
            resultdoc = tmpDoc.get();
            bRtn = true;
        }
    }

    return bRtn;
}

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.