问题描述
这是在将文档的对象插入到集合之前构图的方式
This is How I am framing the document's object before inserting it to the collection
function newTupple(name, email){
return {
email:email,
name:name,
account_status:{
email_verified:{ //I want this in Int64
"$numberLong": `${Date.now()}`
},
activated:false
}
}
}
然后我将其插入
const new_doc = newTupple("Ninja", "[email protected]")
collection.insertOne(new_doc, (err, result)=>{
if(err){
//err: returns " key $numberLong must not start with '$' "
}
}
开头我正在使用npm软件包 [email protected]
I am using npm package [email protected]
我在做什么错?还是自定义数据类型不适用于JS?
What am I doing wrong? Or is custom data type not available for JS?
......................................
......................................
更新
.................................... ..
......................................
Fu ll错误日志
Error: key $numberLong must not start with '$'
at serializeInto (--------------\node_modules\bson\lib\bson\parser\serializer.js:915:19)
at serializeObject (--------------\node_modules\bson\lib\bson\parser\serializer.js:347:18)
at serializeInto (--------------\node_modules\bson\lib\bson\parser\serializer.js:941:17)
at serializeObject (--------------\node_modules\bson\lib\bson\parser\serializer.js:347:18)
at serializeInto (--------------\node_modules\bson\lib\bson\parser\serializer.js:941:17)
at serializeObject (--------------\node_modules\bson\lib\bson\parser\serializer.js:347:18)
at serializeInto (--------------\node_modules\bson\lib\bson\parser\serializer.js:727:17)
at serializeObject (--------------\node_modules\bson\lib\bson\parser\serializer.js:347:18)
at serializeInto (--------------\node_modules\bson\lib\bson\parser\serializer.js:941:17)
at BSON.serialize (--------------\node_modules\bson\lib\bson\bson.js:64:28)
推荐答案
您正在使用扩展的json不适合在应用程序中构造对象图的语法。相反,您应该构造适当类型的对象。
You are using extended json syntax which is inappropriate for constructing object graphs in an application. Instead you should be constructing objects of appropriate types.
For example see here for a Long example.
const BSON = require('bson');
const Long = BSON.Long;
const doc = { long: Long.fromNumber(100) };
这篇关于我可以在mongodb的NodeJS驱动程序中手动指定BSON类型吗?如果我使用“ $ numberLong”,则会出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!