Inserting C++ object into MongoDB collection

Not sure if the tags, or even the topic is right but if it isn’t please let me know and I will change it.

Otherwise, I have a pretty simple question. In Java, I am able to insert custom class objects such as Person, Customer, etc. into the collection by using the following code

Person person;
MongoCollection<Person> col = db.getCollection("MachineData", Person.class);
col.insertOne(person);

Although, I cannot seem to figure out how to accomplish this within C++. Would this be more of a question for a C++ forum? Is this not how it is intended to be used being Mongo isn’t an ORDB?

Currently, I have this

        // Creating customer object
        Customer *cust = new Customer("Apple", "California", "Los Angeles", "1 Apple Way", "00000", "Customer Name", "012-345-6789", "email@yahoo.com");

        // Grabbing the collection
        mongocxx::collection collection = db["Quotes"];

        // Make the document to insert
        auto doc_value = bsoncxx::builder::basic::make_document(
            bsoncxx::builder::basic::kvp("name", "Name"),
            bsoncxx::builder::basic::kvp("type", "database"),
            bsoncxx::builder::basic::kvp("count", 1),
            bsoncxx::builder::basic::kvp("versions", bsoncxx::builder::basic::make_array("v6.0", "v5.0", "v4.4", "v4.2", "v4.0", "v3.6")),
            bsoncxx::builder::basic::kvp("info", bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("x", 203), bsoncxx::builder::basic::kvp("y", 102))));
        auto doc_view = doc_value.view();

        collection.insert_one(doc_view);

Which I generally received from this link, following along with the sections. If this is possible, I’d love to be lead into the direction of how to go about it. Thanks a lot!

Edit:

So I should have realized this prior to posting, but I noticed that

bsoncxx::builder::basic::kvp("info", bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("x", 203), bsoncxx::builder::basic::kvp("y", 102))));

creates an embedded document, which is somewhat of the object I am looking for because I believe? Is this the only way to accomplish how I did it in Java?

Got it.

I just had to make a docifyObject method within the class.

Basically taking every field, and making it into an embedded document within the document itself, being there were three objects to one document in my case. Then when receiving this object from a find method, you have to put them into the class variables one by one, I called this objectifyDocument (sure there is a better name for that lol).

void Quote::objectifyDocument(std::optional<bsoncxx::document::value> jsonData) {
    setQuoteNumber(jsonData->view().find("quoteNumber")->get_string().value);
    rep.setRepName(jsonData->view().find("Rep")->get_document().view().find("repName")->get_string().value);
    rep.setRepPhone(jsonData->view().find("Rep")->get_document().view().find("repPhone")->get_string().value);
    rep.setRepEmail(jsonData->view().find("Rep")->get_document().view().find("repEmail")->get_string().value);
    specs.setMachineType(jsonData->view().find("Specifications")->get_document().view().find("type")->get_string().value);
    specs.setTableSize(jsonData->view().find("Specifications")->get_document().view().find("size")->get_string().value);
    specs.setBeginningDeliveryTime(jsonData->view().find("Specifications")->get_document().view().find("beginningDeliveryTime")->get_string().value);
    specs.setEndingDeliveryTime(jsonData->view().find("Specifications")->get_document().view().find("endingDeliveryTime")->get_string().value);
    customerInfo.setCompanyName(jsonData->view().find("Customer Information")->get_document().view().find("companyName")->get_string().value);
    customerInfo.setCustomerState(jsonData->view().find("Customer Information")->get_document().view().find("customerState")->get_string().value);
    customerInfo.setCustomerCity(jsonData->view().find("Customer Information")->get_document().view().find("customerCity")->get_string().value);
    customerInfo.setCustomerAddress(jsonData->view().find("Customer Information")->get_document().view().find("customerAddress")->get_string().value);
    customerInfo.setCustomerZipCode(jsonData->view().find("Customer Information")->get_document().view().find("customerZipCode")->get_string().value);
    customerInfo.setCustomerName(jsonData->view().find("Customer Information")->get_document().view().find("customerName")->get_string().value);
    customerInfo.setCustomerPhone(jsonData->view().find("Customer Information")->get_document().view().find("customerPhone")->get_string().value);
    customerInfo.setCustomerEmail(jsonData->view().find("Customer Information")->get_document().view().find("customerEmail")->get_string().value);
}

Then with the docifyObject you basically just make a document with your object name as the field of the embedded document, and use getters to set the values of the document fields.

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