I try to insert a long/BigInt into my MongoDB Database with the Node.js Mongo Driver.
I've already tried with BigInt, but it doesn't insert the number (expiry) in the document.
let expire = BigInt(parseInt((Date.now() / 1000) + 21600, 10));
let newDoc = {
type: "group.test",
expiry: expire
};
collection.insertOne(newDoc);
// it only inserts the type.
I want it to save as BigInt because we need to get it later with a Java Server.
BigInt is an object in JS you can't just pass it to Mongodb. Take a look at the data types supported by Mongodb.
https://docs.mongodb.com/manual/reference/bson-types/
I would suggest storing the BigInt as string in mongodb and let the reader parse it when the document is read.
// Note: this function is a simple serializer
// meant to demo the concept.
function bigIntSerializer(num){
return {
type: "BigInt",
value: num.toString()
};
}
let expire = BigInt(parseInt((Date.now() / 1000) + 21600, 10));
let newDoc = {
type: "group.test",
expiry: bigIntSerializer(expire)
};
collection.insertOne(newDoc);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With