MongoDB可視化操作軟件钳宪,是使用圖形界面操作數(shù)據(jù)庫的一種方式。
數(shù)據(jù)庫相關(guān)概念
Mongoose第三方包
使用Node.js操作MongoDB數(shù)據(jù)庫需要依賴Node.js第三方包mongoose
使用npm install mongoose 命令下載寨辩。
啟動MongoDB-
在命令行工具中運行net start mongoDB 即可啟動MongoDB,否則MongoDB將無法連接。
數(shù)據(jù)庫連接-
使用mongoose提供的connect方法即可連接數(shù)據(jù)庫歼冰。
mongoose.connect('mongodb://localhost/playground')
? ? ? ? ? ? .then( () => console.log('數(shù)據(jù)庫連接成功'))
? ? ? ? ? ? .catch( err => console.log('數(shù)據(jù)庫連接失敗', err));
創(chuàng)建數(shù)據(jù)庫
在MongoDB中不需要顯示創(chuàng)建數(shù)據(jù)庫靡狞,如果正在使用的數(shù)據(jù)庫不存在,MongoDB會自動創(chuàng)建隔嫡。
創(chuàng)建集合
創(chuàng)建集合分為兩步甸怕,一是對集合設(shè)定規(guī)則甘穿,二是創(chuàng)建集合,創(chuàng)建mongoose.Schema 構(gòu)造函數(shù)的實例即可創(chuàng)建集合梢杭。
//設(shè)定集合規(guī)則
const courseSchema = new mongoose.Schema ({
? ? ? name : String,
? ? ? author : String,
? ? ? isPublished : Boolean
});
//創(chuàng)建集合并應(yīng)用規(guī)則
const Course = mongoose.model('Course', courseSchema);
//course
創(chuàng)建文檔
創(chuàng)建文檔實際上就是向集合中插入數(shù)據(jù)温兼。
分為兩步:創(chuàng)建集合實例,調(diào)用實例對象下的save方法將數(shù)據(jù)保存到數(shù)據(jù)庫中武契。
//創(chuàng)建集合實例
const course = new Course ({
? ? ? ? name : 'Node.js course',
? ? ? ? author : String,
? ? ? ? tag : ['node','backend'],
? ? ? ? isPublished : true
});
//將數(shù)據(jù)保存到數(shù)據(jù)庫中
course.save ();
Course.create({name : 'JavaScript基礎(chǔ)', author : '黑馬講師', isPublish : true}, (err,doc) => {
? ? ? //錯誤對象
? ? ? console.log(err)
? ? ? //當前插入的文檔
? ? ? console.log(doc)
});
Course.create({name : 'JavaScript基礎(chǔ)', author : '黑馬講師', isPublish : true})
? ? ? .then(doc => console.log(doc))
? ? ? .catch(err => console.log(err))
MongoDB數(shù)據(jù)庫導(dǎo)入數(shù)據(jù)
mongoimport -d 數(shù)據(jù)庫名稱 -c 集合名稱 -file 要導(dǎo)入的數(shù)據(jù)文件
找到mongodb 數(shù)據(jù)庫的安裝目錄募判,將安裝目錄下的bin目錄放置在環(huán)境變量中。
查詢文檔
//根據(jù)條件查找文檔(條件下為空則查找所有文檔)
Course.find().then(result => console.log(result))
//返回文檔集合
[{
? ? _id : 3762897299,
? ? name : 'node.js基礎(chǔ)',
? ? author : '黑馬講師'
},{
? ? _id : 3762897298,
? ? name : 'javascript',
? ? author : '黑馬講師'
}]
//根據(jù)條件查找文檔
Course.findOne({name : 'node.js基礎(chǔ)'}).then(result => console.log(result));
//返回文檔
{
? ? _id : 3762897299,
? ? name : 'node.js基礎(chǔ)',
? ? author : '黑馬講師'
}
//匹配大于吝羞,小于
User.find({age: {$gt : 20, $lt : 50}}).then(result => console.log(result))
//匹配包含
User.find({hobbies : {$in : ['敲代碼']}}).then(result => console.log(result))
//選擇要查詢的字段
User.find().select(' name email').then(result => console.log(result))
//將數(shù)據(jù)按照年齡進行排序
User.find().sort('age').then(result => console.log(result))
//skip跳過多少條數(shù)據(jù)兰伤,limit限制查詢數(shù)量
User.find().skip(2).limit(2).then(result => console.log(result))
刪除文檔
//刪除單個
Course.findOneAndDelete({}).then(result => console.log(result))
//刪除多個
User.deleteMany({}).then(result => console.log(result))
更新文檔
User.updateOne({查詢條件},{要修改的值}).then(result => console.log(result))
User.updateMany({查詢條件},{要修改的值}).then(result => console.log(result))
mongoose驗證
在創(chuàng)建集合規(guī)則時,可以設(shè)置當前字段的驗證規(guī)則钧排,驗證失敗就則輸入插入失敗敦腔。
require:true? 必傳字段
minlength:3? 字符串最小長度
maxlength:20? 字符串最大長度
min : 2? 數(shù)值最小為2
max : 100數(shù)值最大為100
enum : ['html','css','javascript','node.js']
trim : true? 去除字符串兩邊空格
validate:自定義驗證器
default: 默認值
獲取錯誤信息:error.errors['字段名稱'].message
集合關(guān)聯(lián)
通常不同集合的數(shù)據(jù)之間是有關(guān)系的,例如文章信息和用戶信息存儲在不同集合中恨溜,但文章是某個用戶發(fā)表的符衔,要查詢文章的所有信息,就需要用到集合關(guān)聯(lián)糟袁。
使用id對集合進行關(guān)聯(lián)
使用populate方法進行關(guān)聯(lián)集合查詢判族。
集合關(guān)聯(lián)實現(xiàn)
//用戶集合
const User = mongoose.model('User', new mongoose.Schema({ name : {type : String}}));
//文章集合
const Post = mongoose.model('Post', new mongoose.Schema({
? ? title : { type : String},
? ? author : { type : mongoose.Schema.Types.ObjectId,ref:'User'}
}));
//聯(lián)合查詢
Post.find()
.populate('author')
.then(err,result) => console.log(result);