我尝试使用 Node.js Mongo 驱动程序将 long/BigInt 插入到我的 MongoDB 数据库中。
我已经尝试过使用 BigInt,但它不会在文档中插入数字(过期)。
let expire = BigInt(parseInt((Date.now() / 1000) + 21600, 10));
let newDoc = {
type: "group.test",
expiry: expire
};
collection.insertOne(newDoc);
// it only inserts the type.
我希望将其保存为 BigInt,因为我们稍后需要使用 Java 服务器获取它。
最佳答案
BigInt 是 JS 中的一个对象,你不能将它传递给 Mongodb。看看Mongodb支持的数据类型。
https://docs.mongodb.com/manual/reference/bson-types/
我建议将 BigInt 作为字符串存储在 mongodb 中,并让读者在阅读文档时解析它。
// 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);
关于javascript - 如何使用 Node.js MongoDB 驱动程序插入 long/BigInt?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57780241/