Inserting an array of objects into through C++

I have an array within Mongo that I had populated, but I am unsure how to populate it using the C++ driver.

image

The is what the array looks like.

I tried the following,

    // Creating the array of line items
    bsoncxx::builder::basic::array lineItemArr = bsoncxx::builder::basic::array{};
    for(LineItem *lineItem: getLineItems()) {
        lineItemArr.appendlineItem);
    }

Where LineItem is an object that has the fields within the array. I am perplexed on how to get this, as when I run this I get the following error

C2338: append is disabled for non-char pointer types

I also tried this, and it crashed without giving an error.

    // Creating the array of line items
    bsoncxx::builder::basic::array lineItemArr = bsoncxx::builder::basic::array{};
    for(LineItem *lineItem: getLineItems()) {
        lineItemArr.append(bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("description", lineItem->getDescription()),
                                                                  bsoncxx::builder::basic::kvp("grouping", lineItem->getGrouping()),
                                                                  bsoncxx::builder::basic::kvp("partNumber", lineItem->getPartNumber()),
                                                                  bsoncxx::builder::basic::kvp("price", lineItem->getPrice())));
    }

Hi @bigorca54

You can find examples that work with array with C++ driver here - Working with BSON
The second piece of code you shared shouldn’t crash unless the lineItem pointer is null/junk. Perhaps try adding a check to ensure the pointer you are using to get the data is valid (null-check?).

1 Like

Yeah turns out it works fine (the last one)… I guess I just had some other small things wrong, I fixed a bunch of stuff that was not shown with the class. Thanks Rishabh, appreciate the link and feedback!

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