小编The*_*KER的帖子

mongoDB 在查询响应中返回 $numberDecimal

下面是我的猫鼬架构

const mongoose = require('mongoose');

const AccountingCostsSchema = mongoose.Schema(
    {
        // other properties goes here
        accountCosts: {
            type: mongoose.Decimal128,
            set: (v) =>
            mongoose.Types.Decimal128.fromString(parseFloat(v).toFixed(4)),
            required: true
        }
    },
    {
        collection: 'accountingCosts'
    }
);

export = mongoose.model('AccountingCosts', AccountingCostsSchema);
Run Code Online (Sandbox Code Playgroud)

MongoDB 中的数据

会计费用收取

文本模式视图中的数据

{
    "_id" : ObjectId("4e1eb38c2bdea2fb81f5e771"),
    "accountId" : ObjectId("4e8c1180d85de1704ce12110"),
    "accountCosts" : NumberDecimal("200.00"),
}
Run Code Online (Sandbox Code Playgroud)

文本模式视图中的数据

文本模式视图中的数据

我的查询

db.getCollection('accountingCosts').find({'accountId': '4e8c1180d85de1704ce12110'})
Run Code Online (Sandbox Code Playgroud)

查询结果

"accountCosts": {
      "$numberDecimal": "123.00"
}
Run Code Online (Sandbox Code Playgroud)

我尝试在模式上编写一个 getter 函数,就像我有一个 setter 函数一样。但它不起作用

get: function(value) {
      return value.toString();
}
Run Code Online (Sandbox Code Playgroud)

我的预期输出只是一个简单的属性,其名称和值如下所示

"accountCosts": "123.00"
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js mongoose-schema

7
推荐指数
1
解决办法
5097
查看次数

标签 统计

mongodb ×1

mongoose ×1

mongoose-schema ×1

node.js ×1