我需要将数据插入到我的mongoDB中,如下所示:
db.collection('Test').insert({
"Name" : "Some",
"UserID" : NumberLong(2147483647),
...
插入应该发生在与mongo db交互的nodejs脚本中 一切都很好,除了NumberLong()。
我收到以下错误:
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)
我尝试了什么:
var BSON = require('bson');
。也许我应该使用BSONElements?var Long = require('mongodb').Long;
- 我应该用长号来代替NumbreLong()('')?有没有办法让NumberLong()工作?由于
答案 0 :(得分:1)
NumberLong仅用于mongo shell。如果你在nodejs(javascript)中使用它并不意味着。 我使用mongoose而只使用数字类型的数据
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');