node.js操作mongodb簡(jiǎn)單示例分享
前兩篇文章我們介紹了nodejs操作redis和MySQL,下面簡(jiǎn)要測(cè)試一下nodejs操作mongodb:
首先安裝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); } });
相關(guān)文章
nodejs中fs模塊三種讀寫(xiě)文件方法的使用區(qū)別
Node.js的fs模塊有多種文件操作API,readFile、read、createReadStream讀取文件,writeFile、write、createWriteStream寫(xiě)入文件,readFile和writeFile將文件寫(xiě)入緩存區(qū),適合小文件操作,read和write分步驟讀寫(xiě),適合大文件,createReadStream和createWriteStream通過(guò)流處理文件2024-10-10nodejs搭建本地服務(wù)器并訪(fǎng)問(wèn)文件操作示例
這篇文章主要介紹了nodejs搭建本地服務(wù)器并訪(fǎng)問(wèn)文件操作,結(jié)合實(shí)例形式分析了nodejs搭建本地服務(wù)器操作步驟及文件訪(fǎng)問(wèn)相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-05-05詳解node單線(xiàn)程實(shí)現(xiàn)高并發(fā)原理與node異步I/O
本篇文章主要介紹了node單線(xiàn)程實(shí)現(xiàn)高并發(fā)原理與node異步I/O ,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09nodejs開(kāi)發(fā)環(huán)境配置與使用
經(jīng)過(guò)幾個(gè)星期的nodejs學(xué)習(xí),從開(kāi)始的小白到現(xiàn)在漸漸得熟悉,走過(guò)來(lái)也才算明白,現(xiàn)在已經(jīng)入門(mén)也掌握了相關(guān)的學(xué)習(xí)方法,今天開(kāi)始記錄下自己學(xué)習(xí)的過(guò)程,以便日后查看。2014-11-11npm?install編譯時(shí)報(bào)"Cannot?read?properties?of?null?(r
這篇文章主要給大家介紹了關(guān)于npm?install編譯時(shí)報(bào)“Cannot?read?properties?of?null?(reading?‘pickAlgorithm‘)“錯(cuò)誤的解決辦法,文中將解決方法介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07no-vnc和node.js實(shí)現(xiàn)web遠(yuǎn)程桌面的完整步驟
這篇文章主要給大家介紹了關(guān)于no-vnc和node.js實(shí)現(xiàn)web遠(yuǎn)程桌面的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Node.js模塊Modules的使用實(shí)戰(zhàn)教程
這篇文章主要介紹了Node.js模塊Modules的使用,模塊就是一個(gè)聲明了裝飾器@Module()的類(lèi)。裝飾器@Module()提供了元數(shù)據(jù),以便讓Nest組織應(yīng)用程序結(jié)構(gòu)2023-04-04node.js中的http.request方法使用說(shuō)明
這篇文章主要介紹了node.js中的http.request方法使用說(shuō)明,本文介紹了http.request的方法說(shuō)明、語(yǔ)法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼需要的朋友可以參考下2014-12-12使用nodejs、Python寫(xiě)的一個(gè)簡(jiǎn)易HTTP靜態(tài)文件服務(wù)器
這篇文章主要介紹了使用nodejs、Python寫(xiě)的一個(gè)簡(jiǎn)易HTTP靜態(tài)文件服務(wù)器,分為nodejs和Python兩個(gè)版本,用類(lèi)似淘寶的CSS、JS文件加載方式處理靜態(tài)文件加載,需要的朋友可以參考下2014-07-07