I would like to utilise the MongoDB Decimal128 Data type in loopback. Note: I don't want to use the Number type.
My model:
{
"name": "Mongoproduct",
"options": {
"validateUpsert": true,
"strictObjectIDCoercion": true,
"relations": {},
"mongodb": {
"collection": "products",
"allowExtendedOperators": true
}
},
"properties": {
"id": {
"type": "String",
"required": true,
"length": null,
"precision": null,
"scale": null
},
"sku": {
"type": "String",
"required": false,
"length": 50,
"precision": null,
"scale": null,
},
"buyprice": {
"type": "object",
"mongodb": {
"dataType": "Decimal128"
}
},
}
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
If I query the data through the REST explorer , a record will look something like this:
{
"id": "5b41a6ac1afe940d900cba75",
"sku": "shGHYB12-60-LOZ",
"buyPrice": {
"$numberDecimal": "20.4927"
},
}
If I try and save the same data through a REST Put request , I get the following:
{
"error": {
"statusCode": 500,
"name": "MongoError",
"message": "The dollar ($) prefixed field '$numberDecimal' in 'buyPrice.$numberDecimal' is not valid for storage.",
"driver": true,
"index": 0,
"code": 52,
"errmsg": "The dollar ($) prefixed field '$numberDecimal' in 'buyPrice.$numberDecimal' is not valid for storage.",
"stack": "MongoError: The dollar ($) prefixed field '$numberDecimal' in 'buyPrice.$numberDecimal' is not valid for storage.\n at Function.MongoError.create (C:\\node\\ztest\\ztest_data\\node_modules\\mongodb-core\\lib\\error.js:31:11)\n at toError (C:\\node\\ztest\\ztest_data\\node_modules\\mongodb\\lib\\utils.js:139:22)\n at C:\\node\\ztest\\ztest_data\\node_modules\\mongodb\\lib\\collection.js:1059:67\n at C:\\node\\ztest\\ztest_data\\node_modules\\mongodb-core\\lib\\connection\\pool.js:469:18\n at process._tickCallback (internal/process/next_tick.js:61:11)"
}
}
I have tried a number of things , different MongoDB types ( decimal , Decimal128 , NumberDecimal etc ) . I have tried putting allowExtendedOperators in the datasource config .
According to the loopback docs https://loopback.io/doc/en/lb3/MongoDB-connector.html#type-mappings:
Type conversion is mainly handled by Mongodb. See ‘node-mongodb-native’ for details
And According to the Native types https://docs.mongodb.com/manual/reference/bson-types/ I should be able to specify "decimal". ( I am using Mongodb 4.0 )
Any help is greatly appreciated.