nodejs和mongodb實踐

來源:互聯網
上載者:User

標籤:

    首先,當然是都安裝了nodejs 和mongodb了。這必須是前提條件。

    現在我們要用nodejs串連mongodb資料庫了。我這裡只是一個非常非常簡單是實踐,初學嘛。更深入的學習之後,我會仔細寫筆記記錄的。自己走過的彎路,遇到的問題,肯定有價值的。好了,不多說了,開始動手吧。

  我是在D盤nodework目錄下建立了一個mytest檔案夾的,然後在裡面建立一個test.js。接著用npm安裝mongodb,在cmd視窗找到建立的檔案夾目錄,命令

 npm install mongodb

 

  這樣只會在mytest目錄裡安裝相關的,一會兒我們可以看到多了node_modules檔案夾,裡麵包含了各種依賴檔案。如

接下來開啟test.js檔案,在裡面寫相關代碼,我偷懶了,複製了別人代碼,只是改了串連的資料庫。建立資料和表格可以看我上篇筆記。這裡不多說了。代碼。

var  mongodb = require(‘mongodb‘);var  server  = new mongodb.Server(‘localhost‘, 27017, {auto_reconnect:true});var  db = new mongodb.Db(‘blog‘, server, {safe:true});//blog是資料庫//串連dbdb.open(function(err, db){    if(!err){        console.log(‘connect db‘);        // 串連Collection(可以認為是mysql的table)        // 第1種串連方式        // db.collection(‘mycoll‘,{safe:true}, function(err, collection){        //     if(err){        //         console.log(err);        //     }        // });        // 第2種串連方式mycoll是表格當然是我這麼說的,在mongodb裡是文檔        db.createCollection(‘mycoll‘, {safe:true}, function(err, collection){            if(err){                console.log(err);            }else{                //新增資料                // var tmp1 = {id:‘1‘,title:‘hello‘,number:1};       //          collection.insert(tmp1,{safe:true},function(err, result){       //              console.log(result);       //          });                    //更新資料                   // collection.update({title:‘hello‘}, {$set:{number:3}}, {safe:true}, function(err, result){                   //     console.log(result);                   // });                   // 刪除資料                       // collection.remove({title:‘hello‘},{safe:true},function(err,result){        //                   console.log(result);        //               });                // console.log(collection);                // 查詢資料                var tmp1 = {title:‘hello‘};                   var tmp2 = {title:‘world‘};//這裡添加兩條資料                   collection.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 ...‘);        // //刪除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);    }});

然後在cmd表單裡輸入node test.js命令

出現結果如下:

就這樣用nodejs串連mongodb資料庫了。

nodejs和mongodb實踐

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.