使用node.js怎么對(duì)mongodb進(jìn)行操作

本篇文章為大家展示了使用node.js怎么對(duì)MongoDB進(jìn)行操作,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、網(wǎng)站空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、西區(qū)網(wǎng)站維護(hù)、網(wǎng)站推廣。

首先安裝nodejs mongodb

npm install mongodb

代碼

var mongodb = require('mongodb');
var server = new mongodb.Server('localhost', 27017, {auto_reconnect:true});
var db = new mongodb.Db('mydb', server, {safe:true});

//連接db
db.open(function(err, db){
  if(!err){
    console.log('connect db');
    // 連接Collection(可以認(rèn)為是MySQL的table)
    // 第1種連接方式
    // db.collection('mycoll',{safe:true}, function(err, collection){
    //   if(err){
    //     console.log(err);
    //   }
    // });
    // 第2種連接方式
    db.createCollection('mycoll', {safe:true}, function(err, collection){
      if(err){
        console.log(err);
      }else{
        //新增數(shù)據(jù)
        // var tmp1 = {id:'1',title:'hello',number:1};
    //     collection.insert(tmp1,{safe:true},function(err, result){
    //       console.log(result);
    //     }); 
          //更新數(shù)據(jù)
          // collection.update({title:'hello'}, {$set:{number:3}}, {safe:true}, function(err, result){
          //   console.log(result);
          // });
          // 刪除數(shù)據(jù)
            // collection.remove({title:'hello'},{safe:true},function(err,result){
    //          console.log(result);
    //        });

        // console.log(collection);
        // 查詢(xún)數(shù)據(jù)
        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);
  }
});

上述內(nèi)容就是使用node.js怎么對(duì)mongodb進(jìn)行操作,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文題目:使用node.js怎么對(duì)mongodb進(jìn)行操作
文章分享:http://muchs.cn/article34/pidhse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、微信小程序網(wǎng)站內(nèi)鏈、虛擬主機(jī)Google、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站