Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BigInt get's returned as undefined but is shown in mongoDB Atlast as Int64 #13791

Closed
2 tasks done
Schoonmoeder opened this issue Aug 28, 2023 · 4 comments · Fixed by #13869
Closed
2 tasks done

BigInt get's returned as undefined but is shown in mongoDB Atlast as Int64 #13791

Schoonmoeder opened this issue Aug 28, 2023 · 4 comments · Fixed by #13869
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@Schoonmoeder
Copy link

Schoonmoeder commented Aug 28, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.4.5

Node.js version

16.20.1

MongoDB server version

7.0.0

Typescript version (if applicable)

5.2.2

Description

The value 14055648105137340n returns undefined if you save it in a BigInt
MongoDB Atlas shows it nicely as an Int64.

The signed type, Int64, can hold values from -9223372036854775808 to 9223372036854775807. The unsigned type, Uint64, can hold values from 0 to 18446744073709551615.

9223372036854775807 <- max range
14055648105137340n <- The value we tried to use doesn't work
1305564810513734n <- This value does work

I added a fix (#13787) with patch package to test if this helps but it didn't help

Steps to Reproduce

export interface IBlock {
  n: number; 
  reward: BigInt;
}

export const Block = model<IBlock>(
  'Block',
  new Schema<IBlock>({
     n: Number,
  reward: BigInt
  }),
);

  let a = await Block.create({n: 1, reward: 14055648105137340n});
  let b = await Block.findOne({n: 1});
  console.log(14055648105137340n);
  console.log('create : ', a);
  console.log('findOne : ', b);

  let c = await Block.create({n: 2, reward: 1305564810513734n});
  let d = await Block.findOne({n: 2});
  console.log(1305564810513734n);
  console.log('create : ', c);
  console.log('findOne : ', d);

Expected Behavior

We expected to get the BigInt up to 9223372036854775807n returned

@vkarpov15 vkarpov15 added this to the 7.5.2 milestone Aug 29, 2023
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Aug 29, 2023
@protodev-rage
Copy link

@vkarpov15 when 7.5.1 will be releases? facing same issue, i see fixed being merged but that fix is not working locally for me

@vkarpov15 vkarpov15 modified the milestones: 7.5.2, 7.5.1 Sep 4, 2023
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Sep 4, 2023
@Schoonmoeder
Copy link
Author

I tried it also with 7.5.0, but it does not work.
hopefully, the next version will help fix this

@Schoonmoeder
Copy link
Author

@vkarpov15 I did make a fix for myself. but I don't feel 100% comfortable that is production-ready.

the file lib/cast/bigint.js
I added to the following block just above: assert.ok(false);
if(typeof val === 'object'){ return BigInt(String(val)); }

or with more checks for long param
if(typeof val === 'object' && Object.keys(val).length === 3){ if(val.low !== undefined && val.high !== undefined && val.unsigned !== undefined){ return BigInt(String(val)); } }

the main issue is how check that it is a new Long("123")

@vkarpov15 vkarpov15 modified the milestones: 7.5.1, 7.5.3 Sep 8, 2023
@IslandRhythms IslandRhythms added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Sep 15, 2023
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Sep 15, 2023

The initial findOne call does not have the reward field

14055648105137340n
create :  {
  n: 1,
  reward: 14055648105137340n,
  _id: new ObjectId("6504d12486883d7655834fca"),
  __v: 0
}
findOne :  { _id: new ObjectId("6504d12486883d7655834fca"), n: 1, __v: 0 }
1305564810513734n
create :  {
  n: 2,
  reward: 1305564810513734n,
  _id: new ObjectId("6504d12486883d7655834fcd"),
  __v: 0
}
findOne :  {
  _id: new ObjectId("6504d12486883d7655834fcd"),
  n: 2,
  reward: 1305564810513734n,
  __v: 0
}
const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  n: Number,
  reward: BigInt
});

const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();
  
  let a = await Test.create({n: 1, reward: 14055648105137340n});
  let b = await Test.findOne({n: 1});
  console.log(14055648105137340n);
  console.log('create : ', a);
  console.log('findOne : ', b);

  let c = await Test.create({n: 2, reward: 1305564810513734n});
  let d = await Test.findOne({n: 2});
  console.log(1305564810513734n);
  console.log('create : ', c);
  console.log('findOne : ', d);
}

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Sep 15, 2023
vkarpov15 added a commit that referenced this issue Sep 17, 2023
fix(document): handle MongoDB Long when casting BigInts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants