snb
snb

Reputation: 681

How to append GUID as bsoncxx::types::b_oid to document using mongocxx driver?

I've done some work with the legacy driver and now I'm upgrading to the new one. But I'm stuck with a problem. I'm trying to append a GUID to a basic document, but in new driver only 12 byte length binary data is allowed. In legacy driver, it accepts 16 byte data, and GUID was converted to 16 byte. Is it possible to convert GUID to byte array of length 12?

typedef struct _GUID {
    unsigned long  Data1;
    unsigned short Data2;
    unsigned short Data3;
    unsigned char  Data4[ 8 ];
} GUID;

GUID Insert code:

void insert_guid(std::string name, const GUID& guid)
{
    convertGUIDtoBinary( guid, binaryGuidData );    //Can't convert to 12 byte. It will convert to 16 byte length

    bsoncxx::types::b_oid oId;
    oId.value = bsoncxx::oid(binaryGuidData, 12);   //How to make it 12 byte length?

    bsoncxx::builder::basic::document  _builder;

    _builder.append(kvp(name, oId));

}

Please, if someone could help me, Thanks..

Upvotes: 0

Views: 834

Answers (1)

xdg
xdg

Reputation: 2995

You can't use a b_oid for a 16-byte GUID, so use a b_binary type instead. If you're sure your UUIDs are RFC-4122 compliant (big-endian within each field) and you want to differentiate them from other binary data, then you can use binary subtype k_uuid. Or, you can just use binary subtype k_binary. It doesn't matter to MongoDB -- it only matters if your application cares to make it matter.

Upvotes: 1

Related Questions