Today I tried to connect to MongoDB database for the first time, and the specific steps are also very simple.
First, install the MongoDB package through the NodeJS running environment, enter the directory to be installed, and execute the statement
After npm install mongodb is successfully installed, test how many connections are established with the database through the following statement to close the database
var mongo = require('mongodb');var host = "localhost";var port = mongo.Connection.DEFAULT_PORT;//Create the Server object of the server where the MongoDB database is located var server = new mongo.Server(host, port, {auto_reconnect:true});//Create the MongoDB database var db = new mongo.Db('node-mongo-example', server, {saft:true});//Database connection operation db.open(function(err, db){ if(err) { console.log('Error connecting to the database'); throw err;} else{ console.log("Successfully established database connection"); db.close(); }});db.on('close',function(err,db){ if (err) {throw err;} else{ console.log("Successfully closed database"); }});Run the file where the above code is located in the node running environment, and the following error occurs:
The default port of the mongodb database is 27017, so I changed the port to the default 27017. After running the file, I still reported an error, as shown below:
Obviously, the error itself is not a problem with the port number attribute, but the inability to connect to the default MongoDB database service. Finally, I finally understood that the above error is that there is no executable file running the database server.
The specific startup method is:
Enter the installation location of MongoDB in the running environment, enter the bin folder, and run the following code:
D:/Mongodb/bin>mongod --dbpath D:/Mongodb/data
Generally speaking, the node.js code above can run normally
But when I first tried, I also encountered a problem, that is, when I started the above database server, it could not start, and there was an error in the feedback information:
2015-12-13T00:49:12.195+0600 I STORAGE [initandlisten] exception in initAndListen: 28663 Cannot start server. The default storage engine 'wiredTiger' is not available with this build of mongod. Please specify a different storage engine explicitly, eg --storageEngine=mmapv1., terminating 2015-12-13T00:49:12.195+0600 I CONTROL [initandlisten] dbexit: rc: 100
Looking for information, it was found that this was caused by version conflicts and the storage path was confusing. The specific solution was to run the following statement in the bin directory:
D:/MongoDB/bin mongod --storageEngine=mmapv1 --dbpath [your-path]
Start the database service again and succeeds.
You can see the following prompt by visiting http://localhost:27017:
It looks like you are trying to access MongoDB over HTTP on the native driver port. It will be able to start successfully!
The quick solution to the above NodeJS error when connecting to MongoDB database is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.