Nodejs and MongoDB Practice

Source: Internet
Author: User
Tags findone install mongodb

First of all, of course, both Nodejs and MongoDB have been installed. This must be a precondition.

Now we're going to use Nodejs to connect to the MongoDB database. I'm just a very, very simple practice, beginner. After more in-depth study, I will carefully write the notes recorded. The detours that you have traversed, the problems that you have encountered, must be of value. All right, no more talk, let's get started.

I created a mytest folder in the D-disk Nodework directory and created a test.js inside it. Then use NPM to install MongoDB, in the cmd window to find the new folder directory, command

NPM Install MongoDB

This will only be installed in the MyTest directory of relevant, in a moment we can see the Node_modules folder, which contains a variety of dependent files. Such as

Next open the Test.js file, write the relevant code inside, I lazy, copied someone else's code, just changed the connection database. Create data and tables to see my last notes. There's not much to say here. Code.

varMongoDB = require (' MongoDB ');varServer =NewMongodb. Server (' localhost ', 27017, {auto_reconnect:true});vardb =NewMongodb. Db (' blog ', server, {safe:true});//Blog is a database//Connect DBDb.open (function(err, db) {if(!err) {Console.log (' Connect DB '); //Connect collection (table that can be thought of as MySQL)        //1th mode of connection        //db.collection (' Mycoll ', {safe:true}, function (Err, collection) {        //if (err) {        //Console.log (err);        //     }        // });        //2nd Connection mode mycoll is the form of course I said so, in MongoDB is the documentDb.createcollection (' Mycoll ', {safe:true},function(Err, collection) {if(Err) {Console.log (err); }Else{                //New Data                //var tmp1 = {id: ' 1 ', title: ' Hello ', number:1};       //Collection.insert (tmp1,{safe:true},function (err, result) {       //Console.log (result);       //          });                    //Update Data                   //Collection.update ({title: ' Hello '}, {$set: {Number:3}}, {safe:true}, function (err, result) {                   //Console.log (result);                   // });                   //Delete Data                       //Collection.remove ({title: ' Hello '},{safe:true},function (err,result) {        //Console.log (result);        //               });                //Console.log (collection);                //Querying Data                varTMP1 = {title: ' Hello '}; varTMP2 = {title: ' World '};//Add two data hereCollection.insert ([Tmp1,tmp2],{safe:true},function(Err,result) {console.log (result);                    }); Collection.find (). ToArray (function(Err,docs) {Console.log (' Find ');                   Console.log (Docs);                    }); Collection.findone (function(Err,doc) {Console.log (' FindOne ');                   Console.log (DOC);             });        }        }); //console.log (' delete ... ');        ////Delete collection        //db.dropcollection (' Mycoll ', {safe:true},function (err,result) {  //if (err) {                        //Console.log (' err: ');        //Console.log (err);        //}else{        //console.log (' OK: ');        //Console.log (result);        //     }  //       }); }Else{console.log (err); }});

Then enter the node Test.js command in the cmd form.

The results appear as follows:

This is the way to connect the MongoDB database with Nodejs.

Nodejs and MongoDB Practice

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.