Node.js操作MongoDB数据库实例分析
本文实例讲述了Node.js操作MongoDB数据库。分享给大家供大家参考,具体如下:
Node.js操作MongoDB
npminit npmimongodb--save
{ "name":"test", "version":"1.0.0", "description":"", "main":"app.js", "scripts":{ "test":"echo\"Error:notestspecified\"&&exit1" }, "author":"", "license":"ISC", "dependencies":{ "mongodb":"^3.1.1" } }
连接数据库
//connect.js constMongoClient=require('mongodb').MongoClient; //ConnectionURL consturl='mongodb://localhost:27017'; //DatabaseName constdbName='mydatabase'; //Useconnectmethodtoconnecttotheserver MongoClient.connect(url,{useNewUrlParser:true},function(err,client){ console.log("Connectedsuccessfullytoserver"); constdb=client.db(dbName); client.close(); });
插入
//insert.js constMongoClient=require('mongodb').MongoClient; //ConnectionURL consturl='mongodb://localhost:27017'; //DatabaseName constdbName='mydatabase'; //插入 varinsertData=function(db,callback){ //获取文档集合 varcollection=db.collection('collection3'); vardata=[{"name":"李二狗001","age":20},{"name":"李二狗002","age":21}]; //插入文档 collection.insert(data,function(err,result){ if(err){ console.log('Error:'+err); return; } callback(result); }) } //Useconnectmethodtoconnecttotheserver MongoClient.connect(url,{useNewUrlParser:true},function(err,client){ console.log("Connectedsuccessfullytoserver"); constdb=client.db(dbName); insertData(db,function(result){ console.log(result); client.close(); }); });
查询
//find.js constMongoClient=require('mongodb').MongoClient; //ConnectionURL consturl='mongodb://localhost:27017'; //DatabaseName constdbName='mydatabase'; //查询 varfindData=function(db,callback){ //获取文档集合 varcollection=db.collection('collection3'); varwhereStr={"name":"李二狗001"}; //查询文档 collection.find(whereStr).toArray(function(err,result){ if(err){ console.log('Error:'+err); return; } callback(result); }) } //Useconnectmethodtoconnecttotheserver MongoClient.connect(url,{useNewUrlParser:true},function(err,client){ console.log("Connectedsuccessfullytoserver"); constdb=client.db(dbName); findData(db,function(result){ console.log(result); client.close(); }) });
修改
//update.js constMongoClient=require('mongodb').MongoClient; //ConnectionURL consturl='mongodb://localhost:27017'; //DatabaseName constdbName='mydatabase'; //修改 varupdateData=function(db,callback){ //获取文档集合 varcollection=db.collection('collection3'); varwhereStr={"name":"李二狗002"}; varupdateStr={$set:{"age":100}}; //修改文档 collection.update(whereStr,updateStr,function(err,result){ if(err){ console.log('Error:'+err); return; } callback(result); }) } //Useconnectmethodtoconnecttotheserver MongoClient.connect(url,{useNewUrlParser:true},function(err,client){ console.log("Connectedsuccessfullytoserver"); constdb=client.db(dbName); updateData(db,function(result){ console.log(result); client.close(); }) });
删除
//delete.js constMongoClient=require('mongodb').MongoClient; //ConnectionURL consturl='mongodb://localhost:27017'; //DatabaseName constdbName='mydatabase'; //删除 vardelData=function(db,callback){ //获取文档集合 varcollection=db.collection('collection3'); varwhereStr={"name":"李二狗002"}; //删除文档 collection.remove(whereStr,function(err,result){ if(err){ console.log('Error:'+err); return; } callback(result); }) } //Useconnectmethodtoconnecttotheserver MongoClient.connect(url,{useNewUrlParser:true},function(err,client){ console.log("Connectedsuccessfullytoserver"); constdb=client.db(dbName); delData(db,function(result){ console.log(result); client.close(); }) });
参考:
https://www.npmjs.com/package/mongodb
https://www.nhooo.com/article/58815.htm
https://www.nhooo.com/article/98813.htm
希望本文所述对大家node.js程序设计有所帮助。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。