我可以在mongodb的NodeJS驱动程序中手动指定BSON类型吗?如果我使用“ $ numberLong”,则会出现错误 [英] I can I manually specify BSON type in mongodb's NodeJS Driver? I am getting error if I use "$numberLong"

查看:222
本文介绍了我可以在mongodb的NodeJS驱动程序中手动指定BSON类型吗?如果我使用“ $ numberLong”,则会出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在将文档的对象插入到集合之前构图的方式

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", "jojo@something.com")
collection.insertOne(new_doc, (err, result)=>{
if(err){
    //err: returns " key $numberLong must not start with '$' "
}
}

开头我正在使用npm软件包 mongodb@3.6.0

I am using npm package mongodb@3.6.0

我在做什么错?还是自定义数据类型不适用于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”,则会出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆