可視化工具:?Robo 3T | Free, open-source MongoDB GUI (formerly Robomongo)
mongodb安裝官網(wǎng):MongoDB: The Developer Data Platform | MongoDB
文檔:安裝 MongoDB - MongoDB-CN-Manual (mongoing.com)
配置環(huán)境變量: 是為了擴(kuò)大調(diào)用命令的范圍
具體步驟:我的電腦點(diǎn)右鍵-屬性-高級系統(tǒng)設(shè)置-環(huán)境變量-系統(tǒng)變量-path
將C:\Program Files\MongoDB\Server\3.2\bin目錄粘貼到path路徑的最后面(不要忘記在前面加上;)
打開c盤 的mongod? 查看里邊是不是有data 沒有的話要自己創(chuàng)建一個(gè)
方法一? ?啟動(dòng)終端數(shù)據(jù)庫
輸入cmd 打開終端? 輸入mongod? ?先創(chuàng)建文件夾 例如asas? 默認(rèn)端口號 27017
?
創(chuàng)建新的了 可以在可視化上看見也可以在終端上?db.zjh.find()? 看見? zjh隨便換
?
// 基本指令:
// 查看數(shù)據(jù)庫:show dbs
// 創(chuàng)建/切換數(shù)據(jù)庫:use dbName
// 查看所在數(shù)據(jù)庫:db
// 查看集合:show collections
// 增刪改查:
// 添加數(shù)據(jù):db.xxx.insert({})
// 例如 db.zjh.insert({name:"sadsa",age:"89"})
// 刪除數(shù)據(jù):db.xxx.deleteOne()
// 清空數(shù)據(jù):db.xxx.remove()
// 例如 db.zjh.remove()
// 修改數(shù)據(jù):db.xxx.update(query, updated)
// 當(dāng)我們修改之后,發(fā)現(xiàn)數(shù)據(jù)丟失了,如果只是想要修改數(shù)據(jù)中的某一個(gè)字段的時(shí)候
// 提供了$set修改器,允許我們只對其中的某一個(gè)字段修改
// 例如 db.zjh.update({name:"zhou"},{age:"1231213131"})
// 查看集合中數(shù)據(jù):db.xxx.find()
// 刪除集合刪除數(shù)據(jù)庫:
// 刪除指定的集合:db[collectionName].drop()
// 刪除指定的數(shù)據(jù)庫:db.dropDatabase()
?
------------------------------------------------------------------------------------------------------------------------------
方法二? 在vscode? ?使用node.js? 啟動(dòng) mongodb數(shù)據(jù)庫??
首先可以在node.modules?找到文檔
?
(1)安裝MongoDB訪問驅(qū)動(dòng)
全局安裝驅(qū)動(dòng):npm install mongodb -g
在當(dāng)前項(xiàng)目中引入:npm install mongodb --save
(2)cmd 運(yùn)行終端接口
(1)運(yùn)行報(bào)錯(cuò)??報(bào)錯(cuò)原因 node版本太高了? 降低版本我降低到14
?
(2)? ?node 版本還是太高了 最后我換成了 12的? 換的方法和上邊一樣
?Accessing non-existent property 'count' of module exports inside circular dependency?
(3)運(yùn)行報(bào)新的錯(cuò)誤? 路徑失效了?
DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect?
建立連接的時(shí)候,添加? { userNewUrlParser: true}
mongoose.connect('mongodb://127.0.0.1:27017/test',{useNewUrlParser:true});
(3)運(yùn)行成功
mongodb? ? 插入文檔 - MongoDB-CN-Manual (mongoing.com)
app.js 的數(shù)據(jù) 路徑配置
// 引入mongodb
const { MongoClient } = require('mongodb');
// 連接數(shù)據(jù)庫
MongoClient.connect('mongodb://127.0.0.1:27017/test',{useNewUrlParser:true}, (err, client) => {
// 確定數(shù)據(jù)庫
let db = client.db('ickt_48');
// 確定集合
let coll = db.collection('students');
// 插入一條數(shù)據(jù)
coll.insertOne({ username: 'xiaoming', age: 121232 }, (err, { result }) => {
console.log(err);
console.log(result);
// 判斷是否插入數(shù)據(jù)成功
if (result.n > 0) {
// 成功之后斷開連接
client.close();
}
})
})
?可視化
?到這為止 簡單的 增已經(jīng)完事了? 為了方便下邊開始了 封裝
------------------------------------------------------------------------------------------------------------------------------
數(shù)據(jù)庫導(dǎo)入導(dǎo)出??
ssss .json的文件是自動(dòng)生成的,同時(shí)也要開啟數(shù)據(jù)庫并輸入指定端口號?
導(dǎo)出數(shù)據(jù)
mongoexport -d 數(shù)據(jù)庫名稱 -c 集合名稱 -o 導(dǎo)出文件名稱
導(dǎo)入數(shù)據(jù)
mongoimport -d 數(shù)據(jù)庫名稱 -c 集合名稱 --file 文件名稱文章來源:http://www.zghlxwxcb.cn/news/detail-708300.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-708300.html
到了這里,關(guān)于(2)數(shù)據(jù)庫mongodb 終端 和 vscode創(chuàng)建數(shù)據(jù)庫 數(shù)據(jù)導(dǎo)入導(dǎo)出的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!