前两篇文章我们介绍了nodejs操作redis和MySQL,下面简要测试一下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(可以认为是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{ //新增数据 // 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); } });
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC
- 藤泽麻衣《空みあげて》[WAV]
- 纯音入心系列纯音乐《中国古筝经典名曲》1CD[MP3][1.3GB]
- 纯音入心系列纯音乐《古筝新奏:岁月静好与筝语,细水流年与筝同》1CD[MP3][846.9MB]
- 670《脆弱敏感小女生》[320K/MP3][27.53MB]
- 曾琳.2011-好好爱我【南方】【WAV+CUE】
- 范晓萱.1995-RAIN【福茂】【WAV+CUE】