【问题标题】:Decimal / Float in mongoose for node.jsnode.js 的 mongoose 中的十进制/浮点数
【发布时间】:2011-08-02 02:38:38
【问题描述】:

我在 node.js / mongoDB / mongoose 上启动了我的第一个测试应用程序,这是一个非常简单的应用程序,旨在在 DB 中创建记录并检索它们。

我创建了一个类似的模型:

var Car = new Schema({
    brand : String,
    speed  : Number,
    date  :  { type: Date, default: Date.now }
});

这很好,除了我希望能够为速度提供一个浮点值而不是整数值。我尝试了 Decimal 和 Float,但它们都不起作用。 我也没有在文档中找到。

有什么想法吗?

【问题讨论】:

  • var car = new Car({brand: "", speed: 0.5});

标签: mongodb node.js mongoose


【解决方案1】:

我已经搜索了一些 found this article stating that 来存储浮点值,您必须使用 Number 类型。您可以在speed 字段中存储任何浮点值。

【讨论】:

  • 非常感谢。但是,在这篇文章中,似乎更多的是相反的方式,即将浮点数强制转换为整数,对吗?抱歉,我可能误解了这件事。
  • @Luc:没错,在关于将浮点数强制转换为整数的文章中。但这意味着您可以将浮点值存储在类型为Number 的字段中。所以Number 类型应该用于两种类型:整数和浮点数。
  • @Bugain13,非常感谢,你说得对。我应该做一些奇怪的事情,因为它一开始就不起作用。非常感谢您的帮助!!!!
  • 您应该被警告:当使用数字和聚合(如对 Mongoose 求和)时,您会遇到 Ulps 问题。 (在点后面的几十个零之后插入一个 1)
  • 它只是将浮点数转换为整数。没有别的
【解决方案2】:

是的,您可以使用 Decimal128 类型。

https://mongoosejs.com/docs/api.html#mongoose_Mongoose-Decimal128

【讨论】:

    【解决方案3】:

    您可以创建自己的自定义。像这样

    'use strict';
    
    const mongoose = require('mongoose');
    
    class DoubleType extends Number {
      constructor(v) {
        super(v);
        this.value = v;
        this._bsontype = 'Double';
      }
    
      toBSON() {
        return this;
      }
    }
    
    class Double extends mongoose.SchemaType {
      constructor(key, options) {
        super(key, options, 'Double');
    
        Object.assign(this.$conditionalHandlers, {
          '$lt': val => this.castForQuery(val),
          '$lte': val => this.castForQuery(val),
          '$gt': val => this.castForQuery(val),
          '$gte': val => this.castForQuery(val),
        });
      }
    
      cast(val) {
        if (val == null) {
          return val;
        }
        if (val._bsontype === 'Double') {
          return new DoubleType(val.value);
        }
    
        const _val = Number(val);
        if (isNaN(_val)) {
          throw new mongoose.SchemaType.CastError('Double',
            val + ' is not a valid double');
        }
        return new DoubleType(_val);
      }
    }
    
    mongoose.Schema.Types.Double = Double;
    mongoose.Types.Double = DoubleType;
    
    module.exports = Double;
    

    来源复制自@mongoosejs/double

    【讨论】:

    • Como faria o uso?
    • @EzequielTavares 哇
    【解决方案4】:

    您可以在 Mongoose Schema 中使用 Decimal128 作为

    speed:{
    type:mongoose.Types.Decimal128
    }
    
    

    【讨论】:

      【解决方案5】:

      虽然 mongoDB 完全支持浮点类型,但 mongoose 只支持整数类型的 Number。如果您尝试使用 mongooses 类型的 Number 保存到 mongoDB 浮点数,它将被转换为字符串。

      要解决这个问题,您需要为 mongoose 加载一些插件,该插件将扩展其值类型。有一些插件最适合货币或日期,但在你的情况下,我会使用https://www.npmjs.com/package/mongoose-double

      更改后的模型如下所示:

      var mongoose = require('mongoose')
      require('mongoose-double')(mongoose);
      
      var SchemaTypes = mongoose.Schema.Types;
      var Car = new Schema({
          brand: { 
              type: String 
          },
          speed: {
              type: SchemaTypes.Double
          },
          date: {
              type: Date, 
              default: Date.now 
          }
      });
      

      希望对你有帮助。

      【讨论】:

      • 不,不会的。您可以将 25.50 等数字存储为数字,而不会存储为字符串
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 2014-11-18
      • 2011-06-07
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多