1

I need to insert data into my mongoDB, like such:

db.collection('Test').insert({
     "Name" : "Some",
     "UserID" : NumberLong(2147483647),
      ...

Inserts should happen from a nodejs script that interacts with mongo db All is well, except for the NumberLong().

I'm getting the following error:

ReferenceError: NumberLong is not defined
    at /root/MongoPolluter/MongoPolluter.js:107:23
    at connectCallback (/root/MongoPolluter/node_modules/mongodb/lib/mongo_client.js:505:5)
    at /root/MongoPolluter/node_modules/mongodb/lib/mongo_client.js:443:13
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
    at process._tickCallback (internal/process/next_tick.js:104:9)

What I have tried:

  • adding var BSON = require('bson'); after installing it. Maybe I should use BSONElements?
  • Read this: MongoDB differences between NumberLong and simple Integer? - from which I go the notion that I could use the NumberLong only from mongo shell? Not sure if that is correct.
  • Also read about this: var Long = require('mongodb').Long; - should I just replace NumbreLong() w/ Long.fromString('')? Is there no way of getting the NumberLong() to work?

Thanks

2

1 Answer 1

1

NumberLong is using for mongo shell only. If you use in nodejs (javascript) it no mean. I use mongoose and only Number type of data

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

var MyNumber = mongoose.model('my_number', { long_number: Number });

var record = new MyNumber({ long_number: 1234556 });
record.save(function (err) {
  if (err) {
    console.log(err);
  } else {
    console.log('ok');
  }
});

// have to defind ObjectId when use even it a default type data of mongodb
var id = mongoose.Types.ObjectId('4edd40c86762e0fb12000003');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.