【问题标题】:Meteor mongo save/insert numbers are converted to stringMeteor mongo 保存/插入数字被转换为字符串
【发布时间】:2016-12-16 12:23:23
【问题描述】:

我刚刚注意到,每当使用预定义变量在 MongoDB 中保存任何内容时,数据类型都会从 Number 和 Boolean 更改为 String

Orders = new Mongo.Collection("Orders");
Orders.update({_id:"12345"},{$set:{price:5.5}) //this works

var price = {price: 5.5};
Orders.update({_id:"12345"},{$set:price}); //this is not working anymore in the mongo collection price is saved as String "5.5" not as a number, same goes for Boolean values

谁能帮我解决最近发生的事情?将整个应用程序更改为使用第一种方法并不是解决方案,因为我的代码生成了数千个更新,这些更新之前运行良好。

【问题讨论】:

  • 你能说明你是如何在流星中定义你的订单模式的吗?
  • 新应用程序也会出现这种情况吗?如果没有,你用的是什么包?

标签: string mongodb meteor insert converter


【解决方案1】:

你可以在声明变量的时候试试这个吗:

var price = {price: Number(5.5)};
Orders.update({_id:"12345"},{$set:price});

通过这种方式,我认为它在尝试更新相关数据时可以确保 5.5 确实是一个数字。对于布尔值,我猜你也可以使用Boolean(true)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多