MongoError:文档验证失败-如何将float和int都插入同一个字段-标记为double? [英] MongoError: Document failed validation - How to insert both float and int into the same field - which is marked as double?

查看:95
本文介绍了MongoError:文档验证失败-如何将float和int都插入同一个字段-标记为double?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在插入mongoDB时遇到问题,因为好的对象没有通过mongoDB验证器.

I'm having problems with inserting into mongoDB because good objects don't pass the mongoDB validator.

更糟糕的是,该错误是一个通用错误:Document failed validation,该错误在大型多嵌套对象中可能使验证失败的地方令人困惑.

To make it worse, the error is a generic: Document failed validation which in a big multi-nested object can make things confusing regarding where exactly this validation fails.

myValidatorIs =
  { validator:
      { $jsonSchema :
          { bsonType: "object"
          , required: [ "price" ]
          , properties:
              { price:
                  { bsonType: "double" // price needs to be a double, tried with decimal also.
                  , description: "must be a double/float and is required"
                  }
              }
          }
      }
  , validationAction: "error"
  , validationLevel: "strict"
  };

db.collection("collection").insertOne({ price : 4.5 }); // this works 
db.collection("collection").insertOne({ price : 4.0 }); // this doesn't - see error below

错误:UnhandledPromiseRejection警告:MongoError:文档验证失败

ERROR: UnhandledPromiseRejectionWarning: MongoError: Document failed validation

我的应用程序需要一些不同的东西,但我在这里通过使用price对其进行了简化.

My app needs something different but i simplified it here by using price.

现在,经过大量的反复试验,我弄清楚了实际发生的情况.并没有上面那么清楚.

Now after lots of trial and error i figured out what actually happens. Was not as clear as above.

基本上在javascript中,4.0(float)被隐式转换为4(integer) 由于此integer不是float,因此验证失败.超级有线的.由于此数据来自外部,因此我无法控制floatinteger. Javascript只知道number.

Basically in javascript the 4.0 (float) is implicitly transformed to 4 (integer) And this integer fails the validation since is not a float. Which is super wired. Since this data comes from outside, i can't control if is float or integer. Javascript only knows about number.

这确实是问题吗?我的意思是,我尝试了很多不同的方法,并且除了这种隐式类型转换以外,我看不到任何其他原因.

Is this indeed the problem? I mean i tried lots of different things and i can't see any other reason other then this implicit type conversion.

因为在验证器中-如果我设置了bsonType : "int"并将其赋予{price: 1}{price: 4.0},则插入操作不会出现错误.

Because in the validator - if i set bsonType : "int" and i give it {price: 1} or {price: 4.0} then the insertion works with no errors.

如何处理此类问题?如何插入{price: 4.0}?

How to deal with this type of issue? How to insert {price: 4.0}?

我还应该包括哪些设置以使该description字段设置为出现在错误消息中?毕竟,如果properties.price.description不能创建更好的错误消息,那么它的目的是什么?

Also what settings should i include to make that description field i set to appear in the error message? After all what is the purpose of properties.price.description if not to create better error messages ?

推荐答案

找到2个解决方案:

1.一种有点连线的方法-因为我在专栏中以mixed types结尾.通常,由于增加了复杂性,您可能不希望使用混合类型-在我的情况下,没有充分的理由将它们视为混合类型.

1. A somewhat wired approach - because i endup with mixed types in my column. In general, you might not want mixed types since adds complexity - and there is no good reason for them to be considered mixed in my case.

基本上可以代替单一类型,而可以使用类型列表,如下所示:

Basically instead of a single type, you can use a list of types like so:

bsonType: "double"bsonType: [ "double", "int" ].

此功能记录在这里: $ types .

myValidatorIs =
  { validator:
      { $jsonSchema :
          { bsonType: "object"
          , required: [ "price" ]
          , properties:
              { price:
                  { bsonType: [ "double", "int" ]  // add "int" in this array here
                  , description: "must be a double/float and is required"
                  }
              }
          }
      }
  , validationAction: "error"
  , validationLevel: "strict"
  };

2.推荐的方法,是在@lvrf

const MongoType_Double = require('mongodb').Double;

myValidatorIs =
  { validator:
      { $jsonSchema :
          { bsonType: "object"
          , required: [ "price" ]
          , properties:
              { price:
                  { bsonType: "double"  // leave this as double
                  , description: "must be a double/float and is required"
                  }
              }
          }
      }
  , validationAction: "error"
  , validationLevel: "strict"
  };

// then use the MongoType_Double constructor like so: 

db.collection("collection").insertOne({ price : MongoType_Double(4.0) }); // no errors..

这也应适用于所有其他类型,例如timestamp等:

This should also work for all the other types like timestamp and such:

这篇关于MongoError:文档验证失败-如何将float和int都插入同一个字段-标记为double?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆