1.node.js Environment Installation
NPM Config Set Registry https://registry.npm.taobao.org
NPM Config Get registry
NPM root-g
2.mongodb-3.2.9 Test (user name password configured)
var Server = require (' MongoDB '). Server;
var Db = require (' MongoDB '). Db;
var server = new Server (' 115.29.171.114 ', 27017, {auto_reconnect:true});
var db = new db (' Test1 ', server, {fsync:true}); Test1 is the database name
var table = "Users"; Users are equivalent to table names
var db_user = ' root ';
var db_pass = ' 12345678 ';
Connecting to a database
Db.open (function (err, db) {
if (err) {
Console.log ("Connection Database failed:" + err);
Return
}
Console.log ("Connection to Database Success");
Account authentication
Db.admin (). Authenticate (Db_user, Db_pass, function (err, res) {
if (err) {
Console.log ("Account Authentication failed:" + err);
Return
} else {
Console.log ("Successful account Authentication");
Query table
Db.collection (table, {safe:true},function (Err, collection) {
if (err) {
Console.log ("Query table failed:" + err);
} else {
Console.log ("Query Table success:" + collection);
Collection.find (). ToArray (function (e, docs) {
if (e) {
Console.log ("Record not Found:" + e);
} else {
Console.log ("Record set:");
Console.log (Docs);
Db.close ();
}
});
}
});
}
});
});
3. The code has been uploaded to GitHub:
http://www.cnblogs.com/wjx0912
node. JS environment installation, and connectivity MongoDB test