I am using MongoDB version 2.4.14 on a Raspberry Pi (running the latest Raspbian). I have a simple test file trying to get mongo to work:
let url = 'mongodb://172.20.0.239:27017/'
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect(url,{ useUnifiedTopology: true }, function(err, db) {
if (err) throw err;
var dbo = db.db(<database>);
var cursor = dbo.collection("Players").find();
if (cursor == null) {
console.log('cursor nulo');
} else {
console.log('cursor n nulo');
var strArray = [];
cursor.each(function (err, item) {
if(item!=null){
strArray.push(item.datetime+' '+item.onlineUsers);
console.log(item.datetime+' '+item.onlineUsers);
}
else{
dbo.close();
}
});
}
});
I am getting the error:
MongoServerSelectionError: Server at 172.20.0.239:27017 reports maximum wire version 0, but this version of the Node.js Driver requires at least 2 (MongoDB 2.6)
Is there something wrong with my code?
Modern MongoDB drivers support MongoDB server 2.6 and newer. You are using a 2.4 server.
If possible I recommend upgrading your server to at least 2.6. If this is not possible, you need to hunt down an old (really old at this point) version of the driver that supports 2.4.
Related
I installed mongoDB localy as a service by tutorial. But if i connected to DB on sample from w3school
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
there is no error. When i opened Compas i didn't see new DB "mydb". But if i inserted some data to mydb across application and wrote result to console.log then are theess data returned. But in compas are still not visible. Did you meet with this problem ? Thanks.
There must be some Meteor 1.3 examples of opening a mongo database using
the native driver. Use the MongoDB Node.JS Driver to access collections in
another local database for example. The new meteor 1.3 guides claim it is all possible.
Here's a slightly more updated answer:
import { MongoClient } from 'mongodb';
// Meteor offsets Mongo port by 1, so in case your application is running on 3000
const connection = await MongoClient.connect('mongodb://localhost:3001/', {
useNewUrlParser: true,
useUnifiedTopology: true,
poolSize: 1,
});
const db = await connection.db('testdb');
// then you may create your collection of choice
db.collection('testcollection');
FYI, you can also directly access the Mongo driver like so
import { MongoInternals } from 'meteor/mongo';
MongoInternals.defaultRemoteCollectionDriver()
.mongo.db
import { mongodb } from 'mongodb';
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect("mongodb://localhost:27017/test",function(err,db) {
var collection = db.collection('test');
var doc1 = {'hello':'doc1'};
var doc2 = {'hello':'doc2'};
var lotsOfDocs = [{'hello':'doc3'}, {'hello':'doc4'}];
collection.insert(doc1);
When I am inserting/updating a document in a collection, is the lock applied on the database or the collection. Suppose I have two collections and they are independant of each other in the same database and wants to do write operations on them concurrently. Is this possible?
Here is the code I am using to test this:
var assert = require('assert'),
MongoClient = require('mongodb').MongoClient,
async = require('async');
var station_list = require('./station_list.json'),
trains_list = require('./trains_list.json');
var stationList = [],
trainsList = [];
var MONGO_URL = 'mongodb://localhost:27017/test';
for(var i=0; i<station_list.stations.length; i++)
stationList.push(station_list.stations[i].station_code);
for(var i=0; i<trains_list.trains.length; i++)
trainsList.push(trains_list.trains[i].code);
console.log('trains : ' + trainsList.length + ' stations : ' + stationList.length);
populateTrains();
populateStations();
function populateTrains() {
async.eachSeries(trainsList, populateTrainDb, function (err) {
assert.equal(null, err);
});
}
function populateTrainDb(code, callback) {
MongoClient.connect(MONGO_URL, function (err, db) {
assert.equal(null, err);
var jsonData = {};
jsonData.code = code;
db.collection('trainsCon').replaceOne(
{'code' : code}, jsonData, {upsert: true, w:1}, function (err, res) {
assert.equal(null, err);
db.close();
callback();
});
});
}
function populateStations() {
async.eachSeries(stationList, populateStationDb, function (err) {
assert.equal(null, err);
});
}
function populateStationDb(code, callback) {
MongoClient.connect(MONGO_URL, function (err, db) {
assert.equal(null, err);
var jsonData = {};
jsonData.code = code;
db.collection('stationsCon').replaceOne(
{'code' : code}, jsonData, {upsert:true, w:1}, function (err, res) {
assert.equal(null, err);
db.close();
callback();
});
});
}
The two json files : station_list.json and trains_list.json have around 5000 entries. So after running the given program I get this error after a while :
C:\Users\Adnaan\Desktop\hopSmart\node_modules\mongodb\lib\server.js:242
process.nextTick(function() { throw err; })
^
AssertionError: null == { [MongoError: connect EADDRINUSE 127.0.0.1:27017]
name: 'MongoError',
message: 'connect EADDRINUSE 127.0.0.1:27017' }
at C:\Users\Adnaan\Desktop\hopSmart\testing.js:52:10
at C:\Users\Adnaan\Desktop\hopSmart\node_modules\mongodb\lib\mongo_client.js:276:20
at C:\Users\Adnaan\Desktop\hopSmart\node_modules\mongodb\lib\db.js:224:14
at null.<anonymous> (C:\Users\Adnaan\Desktop\hopSmart\node_modules\mongodb\lib\server.js:240:9)
at g (events.js:273:16)
at emitTwo (events.js:100:13)
at emit (events.js:185:7)
at null.<anonymous> (C:\Users\Adnaan\Desktop\hopSmart\node_modules\mongodb-core\lib\topologies\server.js:301:68)
at emitTwo (events.js:100:13)
at emit (events.js:185:7)
When I check the number of entries entered the database, around 4000 entries had already been entered in both the collections. So what I get from the above experiment was that an error might have occured when one write was being attempted while inside other collection a document must have been getting written.
So how should I proceed to have this concurrency without conflicting locks.
The answer to this question can be quite long and depends on various factors (MongoDB version, storage engine, type of operations you are doing, sharding, etc.). I can only recommend you to read carefully the Concurrency section of the MongoDB documentation, and in particular the lock granularity part.
Make sure to choose the right version of MongoDB first as the behaviour varies greatly from one version to another (e.g. database locking in pre-3.0 vs. collection locking for most operations in post-3.0 using NMAPv1).
I don't think it's concurrency issue with MongoDB, but I could be driver or even with test itself.
I have created a sample application couple of weeks ago to stress test MongoDB while working on a nasty bug. I used C# and MongoDB 3.0 on Windows 10. I have inserted million of documents in multithreaded environment but couldn't crash MongoDB.
Parallel.For(0, 10000, (x =>
{
var lstDocs = new List<BsonDocument>();
for (var i = 0; i < 100; i++)
{
lstDocs.Add(new BsonDocument(doc));
}
collection.InsertMany(lstDocs);
lstDocs.Clear();
}));
You can find code in gist here.
You should not be calling MongoClient.connect every time. That's causing a ton of connections to open and close all the time which is overloading mongo. You should let the MongoClient manage the connection pool. Change it so that you store the db object from MongoClient.connect. Something like this:
var db
MongoClient.connect(url, function(err, database){
db = database;
}
In meteor, if you start meteor mongo, you get a shell, where you can access the database purely as mongo.
project :: (master*) ยป meteor mongo
MongoDB shell version: 2.6.7
connecting to: 127.0.0.1:3001/meteor
Mongo-Hacker 0.0.4
meteor:PRIMARY> db.users.find().count()
6
meteor:PRIMARY>
I want to access the item that is like the db, but with Meteor. For a simple example,
function getTableEntityCount(tablename) {
return db[tablename].find().count();
}
Is this possible in Meteor?
You can try with the mongodb native driver.
// Server code
//Typical require
var mongodb = Meteor.npmRequire("mongodb"), //using arounfa meteorhacks:npm
db = mongodb.Db,
mongoclient = mongodb.MongoClient,
Server = mongodb.Server,
db_connection = new Db('cats', new Server("127.0.0.1", 27017 {auto_reconnect: false}));
db.open(function(err, db) {
db.authenticate('<username>', '<password>', function(err, result) {
//return db[tablename].find().count();
});
});
This is just conceptual code.
I think now you can use this method:
MyCollection.rawCollection()
https://docs.meteor.com/api/collections.html#Mongo-Collection-rawCollection
I am new to mongodb and node.js. I have a simple node.js app that in order to run I have to run the command
mongod
to make sure mongodb is running.
locally this works fine.
now I am moving to heroku, I createrd a Procfile where I declare :
web: node http.js
so this take care of starting node.js.
Now how can I call the command
mongod
on heroku?
Update
before the heroku requirement this is how I used to initialize my mongodb :
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure,
ObjectID = mongo.ObjectID;
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('sampledb', server);
db.open(function(err, db) {
if(!err) {
console.log("Connected to 'sampledb' database");
db.collection('sample', {strict:true}, function(err, collection) {
if (err) {
console.log("The 'sample' collection doesn't exist. Creating it with sample data...");
populateDB();
}
});
}
});
in order to accomodate heroku here is how I initialize mongodb :
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure,
ObjectID = mongo.ObjectID;
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/sampledb';
mongo.Db.connect(mongoUri, function (err, db) {
db.collection('sample', function(er, collection) {
if (err) {
console.log("The 'sample' collection doesn't exist. Creating it with
sample data...");
populateDB();
}
});
});
Now this throws an error because db is not recognized. I think I am missing a conceptual issue here, I am new to this world all together.
any help is appreciated.
You don't call the command mongod on heroku.
You'd add one of the mongo add-ons from addons.heroku.com to your app, then have your code connect to the credentials in the config variable exported to your app by the add-on provider.