Search code examples
javascriptmongodbbinarysubtype

Change binary subtype in MongoDB


Is it possible to change only the subtype of the Binary element in MongoDB?
I have Binary element in database of type 0x00
BinData(0, xxx)
Is it possible via shell to update only the subType part of this element/change it to another value?

Or the only way is to create new property with old binary part?

new BinData(newVal, xxx)

Solution

  • No the only way is to create new BinData using the previous base64 value. Of course to get that value you only need to call the .base64() method.

    > var bin = new BinData(3,"ASNFZ4mrze/+3LqYdlQyEA==")
    > var newType = 0;
    > bin = new BinData(newType, bin.base64());
    BinData(0,"ASNFZ4mrze/+3LqYdlQyEA==")
    

    The reason is that the type property is not writable as shown by the output of Object.getOwnPropertyDescriptor()

    > Object.getOwnPropertyDescriptor(bin, 'type')
    {
            "configurable" : true,
            "enumerable" : false,
            "value" : 0,
            "writable" : false
    }
    

    For example:

    > bin.type;
    0
    > bin.type = 3;
    3
    > bin.type;
    0
    

    You can see that value of bin.type remains unchanged even after setting it to 3