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

Mindaugas Bernatavičius
  • 3,757
  • 4
  • 31
  • 58

1 Answers1

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');
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317