提问者:小点点

mongo模式中具有相同值两个数字


我将mongodb用于一个不协调的bot,我有一个用于bot所在的公会的模式。但是当我使用它时,log和id属性是相同的

我的代码:

const testId = 745708447349342200;    
var testGuild = new Mongoose.Guild({
      authorized: [756541009621680249],
      name: "test Bot",
      id: tesstId,
      defautlMessage: "{user} à quitté le server",
      log: 745708447349342213,
    });
    console.log(testGuild.log); //display 745708447349342200
    Mongoose.Save(testGuild);

我的架构

const GuildSchema = new Schema({
  name: { type: String },
  id: { type: Number, unique: true },
  authorized: [{ type: Schema.Types.Number }],
  defaultMessage: { type: String },
  log: { type: Number, unique: true },
});

我尝试删除模式中的unique:true,但它没有改变任何东西。你有解决办法吗?


共1个答案

匿名用户

您需要将类型更改为string,因为Js对一个很大的数字进行舍入。这不是Mongo的问题,而是JS的问题。

尝试在浏览器控制台中键入

x = 745708447349342213

x

你会看到的

745708447349342200

要解决此问题,只需将模型中的number更改为string,并对字符串进行如下操作:

x = `745708447349342213`

在JS中

745708447349342213 === 745708447349342200

赋予true。不存在此问题的int的最大值为:number.max_safe_integer且等于9007199254740991