首先安装nodejs mongodb
npm install mongodb
var mongodb = require('mongodb'), http = require('http'), url = require('url'); var server = new mongodb.Server('localhost', 27017, {auto_reconnect: true}); var db = new mongodb.Db('mydb', server, {safe: true}); // 插入表数据 function insert(tablename,rowInfo,callback){ db.open( function(err, db){ if(!err){ // db.createCollection db.createCollection(tablename,{safe: true}, function(err, collection){ if(!err){ collection.insert(rowInfo,{safe: true}, function(err, result){ if(!err){ callback( true); } else{ callback( false); } }); } else{ console.log(err); } }); } else{ console.log('connect db'+err); } }); } function save(tablename,rowInfo,callback){ db.open( function(err,db){ if(err) return console.error(err); console.log('* mongodb connected'); db.collection(tablename).save(rowInfo, function(err,result){ callback(result); db.close(); }); }) } // 修改表数据 function update(tablename,id,rowInfo,callback){ db.open( function(err, db){ if(!err){ db.createCollection(tablename,{safe: true}, function(err, collection){ if(!err){ collection.update(id,rowInfo,{safe: true}, function(err, result){ if(!err){ callback( true); } else{ callback( false); } }); } else{ console.log(err); } }); } else{ console.log('connect db'+err); } }); } // 删除表数据 function del(tablename,id,callback){ db.open( function(err, db){ if(!err){ db.collection(tablename,{safe: true}, function(err, collection){ if(!err){ collection. delete(id,{safe: true}, function(err, result){ if(!err){ callback( true); } else{ callback( false); } }); } else{ console.log(err); } }); } else{ console.log('connect db'+err); } }); } // 查询表数据 function find(tablename,callback){ db.open( function(err, db){ if(!err){ db.collection(tablename,{safe: true}, function(err, collection){ if(!err){ collection.find().toArray( function(err, result){ if(!err){ callback(result); } else{ console.log(err); } }); } else{ console.log(err); } }); } else{ console.log('connect db'+err); } }); } // 删除Collection function dropCollection(tablename,callback){ db.open( function(err, db){ if(!err){ db.dropCollection(tablename,{safe: true}, function(err,result){ if(!err){ callback( true); } else{ callback( false); } }); } else{ console.log('connect db'+err); } }); } var myres = ""; http.createServer( function(req,res){ var arg = url.parse(req.url, true).query; var pathname = url.parse(req.url).pathname; console.log(pathname); switch(pathname){ case '/find': find("tb_user", function(data){ myres = data; console.log(data); }); break; case '/insert': var rowInfo = {'userID':10008,'userName':'ciwei','password':'123456','age':25}; insert("tb_user",rowInfo, function(data){ myres = data; }); break; case '/update': var id = {"userID":10008}; var rowInfo = {$set:{"userName":"ciwei88888","password":"123456","age":25}}; update("tb_user",id,rowInfo, function(data){ myres = data; }); break; } var str = arg.callback + '(' + JSON.stringify(myres) + ')' console.log(str); res.end(str); }).listen(8080,'localhost'); console.log("run on 8080");
更多资料请参见nodejs mongodb官网 和mongodb官网