目錄
介紹
概念解析
??????????????????安裝
啟動(dòng)時(shí)注意事項(xiàng)
NodeJs操作Mongo
介紹
MongoDB 是一個(gè)基于分布式文件存儲(chǔ)的數(shù)據(jù)庫。由 C++ 語言編寫。旨在為 WEB 應(yīng)用提供可擴(kuò)展的高性能數(shù)據(jù)存儲(chǔ)解決方案。? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? MongoDB 是一個(gè)介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是非關(guān)系數(shù)據(jù)庫當(dāng)中功能最豐富,最像關(guān)系數(shù)據(jù)庫的。
概念解析
安裝
1:安裝mongodb-win32-x86_64-3.2.5-signed.msi
2:手動(dòng)創(chuàng)建目錄 如 C:\data\db ? 及 ?C:\data\dbConf
??????我們希望將數(shù)據(jù)庫及相關(guān)配置和日志等放入這兩個(gè)目錄中
3:管理員身份運(yùn)行 cmd
?? ?C:\Windows\System32\cmd.exe ? (找到該軟件,右鍵,管理員身份運(yùn)行)?? ?
4:進(jìn)入安裝文件夾
?? ?cd C:\Program Files\MongoDB\Server\3.2\bin
????????注意跨盤符的問題
5:執(zhí)行命令,數(shù)據(jù)庫存儲(chǔ)位置
?? ?mongod.exe --dbpath C:\data\db? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 注意:有時(shí)候第5步執(zhí)行后,有可能dos窗口無法輸入內(nèi)容,需要關(guān)掉窗口重新打開,執(zhí)行第6步;(或ctrl+c終止當(dāng)前命令)
6:執(zhí)行命令,日志和服務(wù)
?? ?mongod.exe --logpath "C:\data\dbConf\mongodb.log" --logappend --dbpath "C:\data\db" --serviceName "YourServiceName" --serviceDisplayName "YourServiceName" --install?? ?? ??
注意:在復(fù)制這一行的時(shí)候,要一行復(fù)制。windows記事本-格式-取消自動(dòng)換行
7:win+r ? > ? ?services.msc
?? ?找到剛才創(chuàng)建的服務(wù),YourServiceName,啟動(dòng)它,如果啟動(dòng)失敗,嘗試重啟電腦。
8:在nodejs的項(xiàng)目中,添加模塊
?? ?npm install mongodb? ? ? ? ? 注意:要先在dos窗口內(nèi)進(jìn)入項(xiàng)目文件夾,然后npm命令將模塊插入
9:編寫app.js
數(shù)據(jù)庫相關(guān)代碼,會(huì)在該頁面后面介紹。
10:node app.js
指開啟服務(wù)
安裝數(shù)據(jù)庫可視化工具
robomongo-0.9.0-rc7-windows-x86_64-2b7a8ca.exe
1:打開該工具
2:開啟連接窗口 MongoDB Connections
3:點(diǎn)擊Create按鈕創(chuàng)建一個(gè)連接,按照默認(rèn)項(xiàng)保存即可
4:選中該連接,點(diǎn)擊連接按鈕
啟動(dòng)時(shí)注意事項(xiàng)
執(zhí)行了 mongod.exe --dbpath C:\data\db 之后打開一個(gè)新管理員窗口到bin路勁下執(zhí)行mongo就是啟動(dòng)mongodb數(shù)據(jù)庫。? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 注意:此方法?mongod.exe --dbpath C:\data\db 可以理解為啟動(dòng)服務(wù),mongo可以理解為連接數(shù)據(jù)庫。啟動(dòng)完之后就關(guān)閉那個(gè)窗口再連接會(huì)報(bào)‘由于目標(biāo)計(jì)算機(jī)積極拒絕,無法連接’,所以此方法不推薦使用
執(zhí)行安裝第六條的命令 mongod.exe --logpath "C:\data\dbConf\mongodb.log" --logappend --dbpath "C:\data\db" --serviceName "YourServiceName" --serviceDisplayName "YourServiceName" --install 之后再從命令行輸入services.msc找到Y(jié)ourServiceName服務(wù),右鍵啟動(dòng)此服務(wù),打開管理員dos窗口到bin路勁下輸入mongo就可以連接mongodb數(shù)據(jù)庫,此方法推薦使用。執(zhí)行了第六條命令(一邊即可),往后需要啟動(dòng)連接,僅需要從命令行輸入services.msc找到 YourServiceName服務(wù),右鍵啟動(dòng)此服務(wù)就可以連接了
NodeJs操作Mongo
進(jìn)入項(xiàng)目路勁下,安裝mongodb插件 npm install mongodb
var MongoClient=require('mongodb').MongoClient;
//.MongoClient是mongodb下的對(duì)象,可以理解為連接對(duì)象
var url = "mongodb://localhost:27017/data"
//url它不是一個(gè)網(wǎng)址,可以理解成數(shù)據(jù)庫的地址,在//的右邊local的左邊可以寫賬戶密碼,端口后面是數(shù)據(jù)庫
MongoClient.connect(url,function(err,db){
if(err) throw err;
console.log('數(shù)據(jù)庫已創(chuàng)建')
db.close() //可以理解為把db關(guān)閉了,關(guān)閉了就不能使用db這個(gè)參數(shù)
})
?以下代碼是做一些簡(jiǎn)單的增刪改查文章來源:http://www.zghlxwxcb.cn/news/detail-697777.html
var MongoClient = require('mongodb').MongoClient //npm i mongodb 安裝驅(qū)動(dòng)
var url = "mongodb://localhost:27017/day0516" //端口后面的data是數(shù)據(jù)庫表示進(jìn)入data數(shù)據(jù)庫
var http = require('http')
http.createServer((req,res)=>{
res.writeHead(200,{
"Content-Type":"text/html;charset=utf-8"
})
//只要訪問這個(gè)JS文件,就直接打開數(shù)據(jù)庫
MongoClient.connect(url,function(err,db){
//一個(gè)電腦里面可以有很多數(shù)據(jù)庫,下面語義是進(jìn)入到data數(shù)據(jù)庫里
var dbase = db.db("day0516")
if(req.url=='/'){
//創(chuàng)建一個(gè)集合(表),集合的命叫userlist,第二個(gè)參數(shù)
dbase.createCollection('userlist',function(err,result){
console.log('服務(wù)創(chuàng)建成功')
})
res.end();
}else if(req.url=='/insert'){
//在數(shù)據(jù)庫中找到表,然后用insertOne插入一條數(shù)據(jù),insertMany可以插入多條數(shù)據(jù)
// var myobj = {"user":"張三","password":"123"}
var myobj = [
{"user":"劉備","password":"111","random":parseInt(Math.random()*1000)},
{"user":"關(guān)羽","password":"222","random":parseInt(Math.random()*1000)},
{"user":"張飛","password":"333","random":parseInt(Math.random()*1000)}
]
dbase.collection('userlist').insertMany(myobj,function(err,result){
console.log('文檔插入成功');
res.end()
})
}else if(req.url=='/select'){
//查詢數(shù)據(jù),排序,數(shù)量
var nul ={} //無條件查詢,查詢所有
var where = {user:'張飛',password:'333'} //查詢條件為user是張飛并且密碼是333
var mysort = {random:-1} //排序 random語義是根據(jù)random字段進(jìn)行排序
// limit(3) 表示返回3條數(shù)據(jù)
// skip(1) 表示跳過1條
dbase.collection('userlist').find(nul).sort(mysort).skip(1).limit(2).toArray(function(err,result){
console.log(result)
result.forEach(data=>{
res.write(data.user+":"+data.password+" - "+data.random+'<br>')
})
res.end()
})
}else if(req.url=='/update'){
//更新數(shù)據(jù) updateOne更新一條數(shù)據(jù) updateMany更新多條數(shù)據(jù)
var whereStr = {"user":"張飛"} //表示更新條件
var updateStr = {$set:{"password":"44444"}} //表示新的值
dbase.collection('userlist').updateOne(whereStr,updateStr,function(err,result){
console.log('文檔更新成功',result);
res.end()
})
}else if(req.url=='/delete'){
//刪除
var whereStr={"user":"張飛"}
dbase.collection('userlist').deleteMany(whereStr,function(err,result){
console.log('文檔刪除成功',result);
res.end()
})
}
})
// res.end()
}).listen(8085)
?文章來源地址http://www.zghlxwxcb.cn/news/detail-697777.html
到了這里,關(guān)于Node.Js篇 NodeJs使用MongoDB的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!