Nodejs method example for connecting to mongodb database, nodejsmongodb
This article describes how to connect to a mongodb database using nodejs. We will share this with you for your reference. The details are as follows:
Var login client = require ('mongodb '). using client; var DB_CONN_STR = 'mongodb: // zlg: 437612lang@110.62.14.243: 27017/lj_node '; using client. connect (DB_CONN_STR, function (err, db) {if (err) {console. log (err)} else {console. log ("connection successful! ");} // Connect to the table var collection = db. collection ("lj_node"); // query the data collection. find (). toArray (function (err, result) {if (err) {console. log ('error: '+ err); return;} else {console. log (result [0]. name)} db. close (); // close link });});
Mongodb. connect (mongodb_url, function (err, client) {// create a linked instance if (err) console. log (err); else {var dbname = "lj_node"; var db = client. db (dbname); // create a database instance var collection = db. collection ('lj _ node'); // create a table instance collection. find ({}). toArray (function (err, docs) {// query the data console. log (docs) client. close (); // close the link });}})
I hope this article will help you design nodejs programs.