nodejs中使用MongoDB數據庫
在nodejs上使用MongoDB,你就必須 先把MongoDB的模塊安裝到項目中,
安裝模塊執行的命令如下:
npm install mongodb
卸載模塊的命令:
npm uninstall mongodb
在nodejs中用require獲取該模塊就可以使用,案例代碼如下:
var mongodb = require("mongodb");var server = new mongodb.Server('localhost',27017,{auto_reconnect:true});
var db = new mongodb.Db("test",server,{safe:false});
db.open(function(err,db){ if(err){ console.log(err); return false; } console.log("We are connected!"); db.collection('test',{safe:true},function(err,collection){ collection.find({unique:{"$exists":true}}).toArray(function(err,items){ if(err){ console.log(err); return false; } for(item in items) console.log(items[item]); process.exit(); }); }); });</pre>
如有轉載,請標明來自此出處http://blog.csdn.net/qxs965266509,必須注意!
本文由用戶 aiguang 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!