node. JS environment installation, and connectivity MongoDB test

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.