Nodejs method example for connecting to mongodb database, nodejsmongodb

Source: Internet
Author: User

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@ 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.

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: 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.