Rishav Bhowmik
Rishav Bhowmik

Reputation: 53

I can I manually specify BSON type in mongodb's NodeJS Driver? I am getting error if I use "$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
  }
}
}

Then I Insert it like

const new_doc = newTupple("Ninja", "[email protected]")
collection.insertOne(new_doc, (err, result)=>{
if(err){
    //err: returns " key $numberLong must not start with '$' "
}
}

I am using npm package [email protected]

What am I doing wrong? Or is custom data type not available for JS?

......................................

Update

......................................

Full Error Log

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)

Upvotes: 0

Views: 3760

Answers (1)

D. SM
D. SM

Reputation: 14480

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) };

Upvotes: 1

Related Questions