如何通过REST Api在Loopback中使用Decimal128

Mar*_*son 7 mongodb node.js 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": {}
}
Run Code Online (Sandbox Code Playgroud)

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

{
    "id": "5b41a6ac1afe940d900cba75",
    "sku": "shGHYB12-60-LOZ",
    "buyPrice": {
      "$numberDecimal": "20.4927"
    },
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试通过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)"
  }
}
Run Code Online (Sandbox Code Playgroud)

我尝试了很多东西,不同的MongoDB类型(decimal,Decimal128,NumberDecimal等).我已经尝试将allowExtendedOperators放在数据源配置中.

根据loopback docs https://loopback.io/doc/en/lb3/MongoDB-connector.html#type-mappings:

类型转换主要由Mongodb处理.有关详细信息,请参阅"node-mongodb-native"

根据Native类型https://docs.mongodb.com/manual/reference/bson-types/ 我应该能够指定"十进制".(我使用的是Mongodb 4.0)

任何帮助是极大的赞赏.

Har*_*hal 0

当您使用 Node.js 时,您可以使用mongoose轻松实现您想要的目标。主要是 Loopback 没有除Number之外的任何其他数据类型(请参见此处)。我想这就是你可以做的。

你可以这样做:

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));
Run Code Online (Sandbox Code Playgroud)

这将输出:

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 }
Run Code Online (Sandbox Code Playgroud)

阅读更多。

希望这对你有帮助!