The first attempt to connect to the MongoDB database today is a simple step.
First, install the MongoDB package through the Nodejs running environment, enter the directory to be installed, execute the statement
NPM install MongoDB After a successful installation, test the connection with the database by using the following statement close the database
var MONGO = require (' MongoDB ');
var host = "localhost";
var port = Mongo. Connection.default_port;
Create the server object
var server = new MONGO for the servers on which the MongoDB database resides. Server (host, port, {auto_reconnect:true});
Create MongoDB database
var db = new MONGO. Db (' node-mongo-example ', server, {saft:true});
Database connection Operations
Db.open (function (err, db) {
if (err) {
console.log (' Error connecting database occurred ');
throw err;}
else{
Console.log ("Successfully established database Connection");
Db.close ();
}
);
Db.on (' Close ', function (err,db) {
if (err) {throw err;}
else{
Console.log ("Successful shutdown of Database");
}
);
Running the file with the above code in the running Environment of node, the following error appears:
The default port for the MongoDB database is 27017, so I changed port to the default 27017, and after running the file, it still complains, as follows:
Obviously, the error itself is not a port number attribute problem, but the default MongoDB database service cannot be connected, and finally it is understood that the reason for the above error is that there is no executable file running the database server.
The specific startup method is:
Enter the MongoDB installation location in the running environment and go to the Bin folder and run the following code:
In general, the above Node.js code will work correctly.
But in the initial attempt, I also encountered a problem, is to start the database server above, in the failure to start, in the feedback message has a error:
2015-12-13t00:49:12.195+0600 I STORAGE [Initandlisten] exception in initandlisten:28663 cannot start server. The default storage engine ' Wiredtiger ' isn't available with this build of Mongod. Please specify a different storage engine explicitly, e.g--storageengine=mmapv1., terminating 2015-12-13t00:49:12.195+ 0600 I control [Initandlisten] dbexit:rc:100
Find data discovery, this is because version conflict caused the confusion of the storage path, the specific solution is to run the following statement in the bin directory:
D:\MongoDB\bin mongod--storageengine=mmapv1--dbpath [Your-path]
Restarting the database service is successful.
You can see the following prompts by accessing http:\\localhost:27017:
It looks like your are trying to access MongoDB over HTTP on the native driver port. It's going to start!
The above Nodejs connection MongoDB database times wrong Fast solution is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.