如何通过REST Api在回送中使用Decimal128

时间:2018-07-09 00:52:08

标签: node.js mongodb loopbackjs

我想在回送中使用MongoDB Decimal128数据类型。注意:我不想使用数字类型。

我的模特:

{
  "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": {}
}

如果我通过REST资源管理器查询数据,则记录将如下所示:

{
    "id": "5b41a6ac1afe940d900cba75",
    "sku": "shGHYB12-60-LOZ",
    "buyPrice": {
      "$numberDecimal": "20.4927"
    },
}

如果我尝试通过REST Put请求保存相同的数据,则会得到以下信息:

{
  "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)"
  }
}

我尝试了很多东西,不同的MongoDB类型(十进制,Decimal128,NumberDecimal等)。我尝试将allowExtendedOperators放在数据源配置中。

根据回送文档https://loopback.io/doc/en/lb3/MongoDB-connector.html#type-mappings

类型转换主要由Mongodb处理。有关详情,请参见“ node-mongodb-native”

然后根据本机类型https://docs.mongodb.com/manual/reference/bson-types/,我应该能够指定“十进制”。 (我正在使用Mongodb 4.0)

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

使用Node.js时,可以使用mongoose轻松实现所需的功能。主要是 Loopback除了 Number see here)之外没有其他数据类型。我认为这是您可以做的。

您可以通过以下方式做到这一点:

var mongoose = require('mongoose');

var testModel = mongoose.Schema({
    "buyprice": {
        "type": mongoose.Schema.Types.Decimal128
    }
}); 
// I have added only the Decimal128 field, you can define your schema here.

const Col = mongoose.model(`test`, testModel);

Col.create({ buyprice: '0.78' }).
    then(doc => console.log('Document: ', doc.toObject())).
    catch(error => console.error(error));

这将输出:

Document:  { _id: 5b4ee53c95eeb452b4239eaf,
  buyprice:
   Decimal128 {
     _bsontype: 'Decimal128',
     bytes: <Buffer 4e 00 00 00 00 00 00 00 00 00 00 00 00 00 3c 30> },
  __v: 0 }

Read more.

希望这对您有帮助!