I'm developing a node.js/mongodb app on Macintosh but I also like my code to work on Windows.
The following code works on the Mac but not on Windows:
var Server = require('mongodb').Server,
Db = require('mongodb').Db;
var db = new Db('bookdb', new Server('localhost', 27017));
db.collection('books').insert({ author: 'bla' });
On Windows I have to add a call to db.open to make it work:
var Server = require('mongodb').Server,
Db = require('mongodb').Db;
var db = new Db('bookdb', new Server('localhost', 27017));
db.open(function(err, db) {
db.collection('books').insert({ author: 'bla' });
});
Does anybody know why this difference exists?
Thanks
Related
i am using a separated module to manage mongodb operations in my nodejs app.
Locally it works well but when i load my app on the production server it says collectionDriver is undefined.
This is where i require the module:
MongoClient = require('mongodb').MongoClient;
Server = require('mongodb').Server;
CollectionDriver = require('./collectionDriver').CollectionDriver;
var mongoClient = new MongoClient(new Server(mongoHost, mongoPort));
mongoClient.open(function(err, mongoClient) {
if (!mongoClient) {
console.error("Error! Exiting... Must start MongoDB first");
process.exit(1);
}
var db = mongoClient.db("MyDatabase");
collectionDriver = new CollectionDriver(db);
});
Local server: 11.18.3.1
Intermediate server: 11.18.3.2
MongoDB Server: 11.18.3.3
Node application is running in 11.18.3.1 server.
We can't connect directly to 11.18.3.3 (MongoDB server) from 11.18.3.1. So first I need to connect 11.18.3.2 then 11.18.3.3. the below code is not working. How can we do this? we need to pass first server stream into the second server.
var client = require('ssh2').Client;
server_ssh = new client();
server_ssh.on('ready', function() {
ts_socket.emit('log', "Server connected");
// from here I want to connect to MongoDB
var MongoClient = require('mongodb').MongoClient
, Server = require('mongodb').Server;
var mongoClient = new MongoClient(new Server('11.18.3.3', 27017));
mongoClient.open(function(err, mongoClient) {
var db1 = mongoClient.db("mydb");
mongoClient.close();
});
}).connect(
{ "host": "11.18.3.2",
"username":"myuser",
"password":"mypassword",
"port":22
}
);
please help.
While going through a mongodb tutorial, I ran into an issue with this configuration:
var MongoClient = require('mongodb').MongoClient;
var mongoClient = new MongoClient(new server('localhost', '27017', {'native_parser': true}))
var db = mongoClient.db('test');
TypeError: Object # has no method 'db'
Eventually, I was able to solve it using mongodb server
var server = require('mongodb').Server,
Db = require('mongodb').Db;
var db =new Db('test', new server('localhost', '27017', {'native_parser': true}));
db.open(function(err, res){
app.listen(8080);
console.dir('app started on 8080');
});
However, the documentation says "Server should not be used, use the MongoClient.connect."
Based on this, I'd like to know when is the appropriate time to use the server?
Here is an example on how to use it in regards to the deprecation present in 2.0 and your setup and usage of callbacks instead of promises:
var mongoDB = require('mongodb');
var theDB = null;
mongoDB
.MongoClient
.connect('mongodb://localhost:27017/test', null, function(err, db) {
if (err) {
console.error(err);
} else {
theDB = db;
app.listen(8080);
console.dir('app started on 8080');
}
});
I am running node.js 10.22, windows 8 and mongodb not sure what version, but I just downloaded it today, when I run my code I am getting a message, please ensure you set the default write concern, I am trying to follow a YouTube video, and there is mention of this, and I am finding little about it on the internet, from what i found, when I set the db i should set j:true, or safe : true/false, but neither not working for me. I do get the console log that I'm connected and the host and port, but then I get the write concern message and can't type or do anything.
var mongo = require('mongodb');
var host = "127.0.0.1";
var port = mongo.Connection.DEFAULT_PORT;
var db = new mongo.Db("nodeintro", new mongo.Server(host,port,{Fsync: true}));
db.open(function(error){
console.log("we are connected"+host + port);
})
Tried this all type of ways as well, still no luck, best i did was get back to the db write concern message, but was not able to even connect this time. What I'm really looking for is to be able to insert anything in mongo db, and i can figure out the rest.
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var BSON = require('mongodb').BSON;
var ObjectID = require('mongodb').ObjectID;
var host = "127.0.0.1";
var port = mongo.DEFAULT_PORT;
ArticleProvider = function(host, port) {
this.db= new Db('node-mongo-blog', new Server(host, port, {auto_reconnect: true}, {}));
this.db.open(function(error){
if(error){
console.log(error)
}
else{
console.log(port,host)
}
});
};
ArticleProvider(host,port)
When using mongodb-native directly, you should now use MongoClient.connect to open a database connection pool. It will set a default write concern for you.
var mongodb = require('mongodb');
mongodb.MongoClient.connect('mongodb://localhost/nodeintro', function(err, db) {
// db is your open nodeintro database connection pool here
});
MongoClient was a somewhat recent addition so the tutorial you're working from likely pre-dates it.
If you use {w:1} parameter in your insert or update operation, you might give this error. To overcome you can use {journal:true} parameter in your db settings.
For instance;
var Db = require('mongodb').Db,
MongoClient = require('mongodb').MongoClient,
Server = require('mongodb').Server,
ReplSetServers = require('mongodb').ReplSetServers,
ObjectID = require('mongodb').ObjectID,
Binary = require('mongodb').Binary,
GridStore = require('mongodb').GridStore,
Grid = require('mongodb').Grid,
Code = require('mongodb').Code,
BSON = require('mongodb').pure().BSON;
var db = new Db('Your DB Name', new Server('192.168.170.128', 27017), { journal : true });
db.open(function(err, db) {
var collection = db.collection('user');
collection.findOne({'_id':req.session.User._id}, function(err, user){
// some codes what do you want
collection.save( user, {w: 1}, function(err, user_id) {
// just close the db connection
db.close();
});
});
});
I am trying to run a Node.js script locally and it's giving me this error message:
========================================================================================
= Please ensure that you set the default safe variable to one of the =
= allowed values of [true | false | {j:true} | {w:n, wtimeout:n} | {fsync:true}] =
= the default value is false which means the driver receives does not =
= return the information of the success/error of the insert/update/remove =
= =
= ex: new Db(new Server('localhost', 27017), {safe:false}) =
= =
= http://www.mongodb.org/display/DOCS/getLastError+Command =
= =
= The default of false will change to true in the near future =
= =
= This message will disappear when the default safe is set on the driver Db =
========================================================================================
Here are my variables:
var express = require('express');
var mongodb = require('mongodb');
var GridStore = require('mongodb').GridStore;
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
var db = new Db(Config.dbName, new Server("127.0.0.1", 27017, {}), {});
var HttpGet = require('./httpGet').HttpGet;
var URL = require('url');
var dbClient = null; // this is initialized when db is opened
var app = module.exports = express();
The same scripts runs fine on my live server. It only complanes when I run it locally.
I found this same issue being discussed on github but found no solution.
https://github.com/kissjs/node-mongoskin/issues/77
Anyone know what could cause this problem?
Thanks in advance :)
The following works for me using the 1.1.11 mongo driver:
var db = new Db(Config.dbName, new Server("127.0.0.1", 27017, {}), {safe: true});
Without the {safe: true} parameter I do get the same warning as you show in your question.
This warning was a very recent addition to the driver; you're probably using an older version of the driver on your server which is why you don't see the warning there.
I got it to work by setting the strict mode to false.
var db = new Db(config.dbName, new Server("127.0.0.1", 27017, {}), {safe: false, strict: false});
This worked for me!
var db = new Db((new DbServer('127.0.0.1', 27017), {w:-2,journal:false,fsync:false,safe: false})