[CXX-983] Misunderstanding about bsoncxx::document::element Created: 05/Aug/16  Updated: 08/Jan/24  Resolved: 08/Aug/16

Status: Closed
Project: C++ Driver
Component/s: API
Affects Version/s: 3.0.0
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Andrey Kolkov Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

ubuntu 16_04 x64, gcc 5.4



 Description   

Is it correct to store bsoncxx::document::element in containers without storing bsoncxx::document::value (parental document)?

I want to store a field from many documents and after transformation it to specific types... Like this:

    std::list<bsoncxx::document::element> dataFields;
    // ...
    mongocxx::cursor::iterator iter = (*current);
    bsoncxx::document::view_or_value doc(*iter);
    dataFields.push_back(doc.view()['data']);
    ++(*current);
    // ...
    for(auto data: dataFields)
    {
        if (data)
        {
            if (data.type() == bsoncxx::type::k_document)
            // everytime is ok
            {
                // ...
            }
 
            if (data.type() == bsoncxx::type::k_binary) 
            // sometimes throws "SIGABRT: src/bson/bson-iter.c:390 bson_iter_type(): precondition failed: iter->raw"
            {
                // ...
            }
 
        }
    }   



 Comments   
Comment by David Golden [ 09/Aug/16 ]

For any pointer type, you need to keep the bsoncxx::document::value alive until you're done working with the data. Alternatively, you can copy the bytes out of the b_binary struct you get from get_binary and then let the document go.

Comment by Andrew Morrow (Inactive) [ 09/Aug/16 ]

Be careful! A bsoncxx::types::value is just a union of the element types, so the same rules apply. You need backing storage for it.

Comment by Andrey Kolkov [ 09/Aug/16 ]

Okay, I found "alternate approach" and I used bsoncxx::types::value instead of bsoncxx::document::element, but problem of converting to binary data preserved.

Comment by David Golden [ 05/Aug/16 ]

A bsoncxx::document::element is a single-field view into a bsoncxx::document::value, so you do need to keep the parents around or come up with an alternate approach.

Generated at Wed Feb 07 22:00:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.