使用mongoose 在 Node中操作MongoDB数据库
MongoDB
關(guān)系型和非關(guān)系型數(shù)據(jù)庫
關(guān)系型數(shù)據(jù)庫(表就是關(guān)系,或者說表與表之間存在關(guān)系)。
- 所有的關(guān)系型數(shù)據(jù)庫都需要通過sql語言來操作
- 所有的關(guān)系型數(shù)據(jù)庫在操作之前都需要設(shè)計表結(jié)構(gòu)
- 而且數(shù)據(jù)表還支持約束
- 唯一的
- 主鍵
- 默認值
- 非空
非關(guān)系型數(shù)據(jù)庫
- 非關(guān)系型數(shù)據(jù)庫非常的靈活
- 有的關(guān)系型數(shù)據(jù)庫就是key-value對兒
- 但MongDB是長得最像關(guān)系型數(shù)據(jù)庫的非關(guān)系型數(shù)據(jù)庫
- 數(shù)據(jù)庫 -》 數(shù)據(jù)庫
- 數(shù)據(jù)表 -》 集合(數(shù)組)
- 表記錄 -》文檔對象
一個數(shù)據(jù)庫中可以有多個數(shù)據(jù)庫,一個數(shù)據(jù)庫中可以有多個集合(數(shù)組),一個集合中可以有多個文檔(表記錄)
{qq:{user:[{},{},{}...]} }- 也就是說你可以任意的往里面存數(shù)據(jù),沒有結(jié)構(gòu)性這么一說
安裝
-
下載
- 下載地址:https://www.mongodb.com/download-center/community
- 下載地址:https://www.mongodb.com/download-center/community
-
安裝
一直Next即可。。。
這是安裝MongoDB的服務(wù)。這是4之后的版本有的,這里安裝了以后,就不用配置dbpath和logpath,也不用配置服務(wù)了。安裝完了,就直接可以使用MongoDB。
- 配置環(huán)境變量
- 最后輸入mongod --version測試是否安裝成功
啟動和關(guān)閉數(shù)據(jù)庫
啟動:
# mongodb 默認使用執(zhí)行mongod 命令所處盼復(fù)根目錄下的/data/db作為自己的數(shù)據(jù)存儲目錄 # 所以在第一次執(zhí)行該命令之前先自己手動新建一個 /data/db mongod如果想要修改默認的數(shù)據(jù)存儲目錄,可以:
mongod --dbpath = 數(shù)據(jù)存儲目錄路徑注意:mongodb 4版本以上,不需要再手動區(qū)啟動服務(wù),默認已經(jīng)自動啟動了
停止:
在開啟服務(wù)的控制臺,直接Ctrl+C; 或者直接關(guān)閉開啟服務(wù)的控制臺。連接數(shù)據(jù)庫
連接:
# 該命令默認連接本機的 MongoDB 服務(wù) mongo退出:
# 在連接狀態(tài)輸入 exit 退出連接 exit基本命令
-
show dbs
- 查看數(shù)據(jù)庫列表(數(shù)據(jù)庫中的所有數(shù)據(jù)庫)
-
db
- 查看當前連接的數(shù)據(jù)庫
- 查看當前連接的數(shù)據(jù)庫
-
use 數(shù)據(jù)庫名稱
- 切換到指定的數(shù)據(jù)庫,(如果沒有會新建)
- 切換到指定的數(shù)據(jù)庫,(如果沒有會新建)
-
show collections
- 查看當前目錄下的所有數(shù)據(jù)表
- 查看當前目錄下的所有數(shù)據(jù)表
-
db.表名.find()
- 查看表中的詳細信息
- 查看表中的詳細信息
二、在Node中如何操作MongoDB數(shù)據(jù)庫
使用官方的MongoDB包來操作
? http://mongodb.github.io/node-mongodb-native/
https://www.npmjs.com/package/mongodb
使用第三方包mongoose來操作MongoDB數(shù)據(jù)庫(推薦)
? 第三方包:mongoose基于MongoDB官方的mongodb包再一次做了封裝,名字叫mongoose,是WordPress項目團隊開發(fā)的。
? https://mongoosejs.com/
mongoose的第一個demo
新建一個項目文件夾:mongoose-demo,然后cmd 執(zhí)行 npm init -y:
執(zhí)行npm install mongoose:
demo1.js:
// 1. 引入包 const mongoose = require('mongoose');// 2. 連接MongoDB 數(shù)據(jù)庫 mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true,useUnifiedTopology: true });// 3. 創(chuàng)建一個模型 // 就是在設(shè)計數(shù)據(jù)庫 // MongoDB是動態(tài)的,非常靈活,只需要在代碼中設(shè)計你的數(shù)據(jù)庫就可以了 // mongoose 這個包就可以讓你的設(shè)計編寫過程變得非常的簡單 const Cat = mongoose.model('Cat', {name: String });// 4. 實例化一個Cat const kitty = new Cat({name: 'Zildjian' });// 5. 持久化保存kitty實例 kitty.save().then(() => console.log('meow'));
mongoose:
官網(wǎng):http:/mongoosejs.com/
官方指南:http:/mongoosejs.com/docs/guide.html
官方API文檔:http:/mongooseis.com/docs/api.html
mongoose基本使用:
1. 設(shè)計Schema發(fā)布Model:
// 1.導(dǎo)包 const mongoose = require('mongoose')const Schema = mongoose.Schema// 2.連接數(shù)據(jù)庫 // 這里指定連接的數(shù)據(jù)庫mydb可以不需要存在,當你插入第一條數(shù)據(jù)之后就會幫你自動創(chuàng)建這個數(shù)據(jù)庫 mongoose.connect('mongodb://localhost/mydb')// 3.設(shè)計集合結(jié)構(gòu)(表結(jié)構(gòu)) // 字段名稱就是表結(jié)構(gòu)中的屬性名稱 // 值 const userSchema = new Schema({username: {type: String,require: true // 必須有該屬性},password: {type: String,require: true},email: {type: String} });// 3.將文檔結(jié)構(gòu)發(fā)布為模型 // mongoose.model() 就是用來將一個架構(gòu)發(fā)布為 model // 第一個參數(shù): 傳入一個大寫名詞單數(shù)字符串,用來表示你的數(shù)據(jù)庫名稱 // mongoose會自動將大寫名詞的字符串生成小寫復(fù)數(shù)的集合名稱 // 例如:這里的User最終會變?yōu)閡sers集合名稱 // 第二個參數(shù): 架構(gòu)Schema // 返回值: 模型構(gòu)造函數(shù) var User = mongoose.model('User',userSchema)// 4.當我們有了模型構(gòu)造函數(shù)之后,就可以使用這個構(gòu)造函數(shù)對users集合中的數(shù)據(jù)進行操作2. 增加數(shù)據(jù)
// 1.導(dǎo)包 const mongoose = require('mongoose')const Schema = mongoose.Schema// 2.連接數(shù)據(jù)庫 // 這里指定連接的數(shù)據(jù)庫mydb可以不需要存在,當你插入第一條數(shù)據(jù)之后就會幫你自動創(chuàng)建這個數(shù)據(jù)庫 mongoose.connect('mongodb://localhost/mydb')// 3.設(shè)計集合結(jié)構(gòu)(表結(jié)構(gòu)) // 字段名稱就是表結(jié)構(gòu)中的屬性名稱 // 值 const userSchema = new Schema({username: {type: String,require: true // 必須有該屬性},password: {type: String,require: true},email: {type: String} });// 3.將文檔結(jié)構(gòu)發(fā)布為模型 // mongoose.model() 就是用來將一個架構(gòu)發(fā)布為 model // 第一個參數(shù): 傳入一個大寫名詞單數(shù)字符串,用來表示你的數(shù)據(jù)庫名稱 // mongoose會自動將大寫名詞的字符串生成小寫復(fù)數(shù)的集合名稱 // 例如:這里的User最終會變?yōu)閡sers集合名稱 // 第二個參數(shù): 架構(gòu)Schema // 返回值: 模型構(gòu)造函數(shù) var User = mongoose.model('User',userSchema)// 4.當我們有了模型構(gòu)造函數(shù)之后,就可以使用這個構(gòu)造函數(shù)對users集合中的數(shù)據(jù)進行操作 var admin = new User({username: 'admin',password: '123456',email: 'admin@admin.com' })admin.save(function(err, ret) {if(err) {console.log('保存失敗')} else {console.log('保存成功!')console.log(ret)} })3. 查詢數(shù)據(jù)
// 1.導(dǎo)包 const mongoose = require('mongoose')const Schema = mongoose.Schema// 2.連接數(shù)據(jù)庫 // 這里指定連接的數(shù)據(jù)庫mydb可以不需要存在,當你插入第一條數(shù)據(jù)之后就會幫你自動創(chuàng)建這個數(shù)據(jù)庫 mongoose.connect('mongodb://localhost/mydb')// 3.設(shè)計集合結(jié)構(gòu)(表結(jié)構(gòu)) // 字段名稱就是表結(jié)構(gòu)中的屬性名稱 // 值 const userSchema = new Schema({username: {type: String,require: true // 必須有該屬性},password: {type: String,require: true},email: {type: String} });// 3.將文檔結(jié)構(gòu)發(fā)布為模型 // mongoose.model() 就是用來將一個架構(gòu)發(fā)布為 model // 第一個參數(shù): 傳入一個大寫名詞單數(shù)字符串,用來表示你的數(shù)據(jù)庫名稱 // mongoose會自動將大寫名詞的字符串生成小寫復(fù)數(shù)的集合名稱 // 例如:這里的User最終會變?yōu)閡sers集合名稱 // 第二個參數(shù): 架構(gòu)Schema // 返回值: 模型構(gòu)造函數(shù) var User = mongoose.model('User', userSchema)// 4.當我們有了模型構(gòu)造函數(shù)之后,就可以使用這個構(gòu)造函數(shù)對users集合中的數(shù)據(jù)進行操作 // 新增數(shù)據(jù)操作 /* var admin = new User({username: 'zepzep',password: '000000',email: 'zep@zep.com' })admin.save(function(err, ret) {if(err) {console.log('保存失敗')} else {console.log('保存成功!')console.log(ret)} }) */// 查詢數(shù)據(jù)操作 // 1.查詢所有數(shù)據(jù) /* User.find(function(err, ret) {if(err) {console.log('查詢失敗')} else {console.log(ret)} }) */// 2.按條件查詢數(shù)據(jù),返回值為數(shù)組 /* User.find({username: 'zepzep' }, function(err, ret) {if (err) {console.log('查詢失敗')} else {console.log(ret)} })*/// 3.按條件查詢數(shù)據(jù),返回值為查找到的匹配的第一個的對象 User.findOne({username: 'zepzep',password: '111' }, function(err, ret) {if (err) {console.log('查詢失敗')} else {console.log(ret)} })4. 刪除數(shù)據(jù)
// 1.導(dǎo)包 const mongoose = require('mongoose')const Schema = mongoose.Schema// 2.連接數(shù)據(jù)庫 // 這里指定連接的數(shù)據(jù)庫mydb可以不需要存在,當你插入第一條數(shù)據(jù)之后就會幫你自動創(chuàng)建這個數(shù)據(jù)庫 mongoose.connect('mongodb://localhost/mydb')// 3.設(shè)計集合結(jié)構(gòu)(表結(jié)構(gòu)) // 字段名稱就是表結(jié)構(gòu)中的屬性名稱 // 值 const userSchema = new Schema({username: {type: String,require: true // 必須有該屬性},password: {type: String,require: true},email: {type: String} });// 3.將文檔結(jié)構(gòu)發(fā)布為模型 // mongoose.model() 就是用來將一個架構(gòu)發(fā)布為 model // 第一個參數(shù): 傳入一個大寫名詞單數(shù)字符串,用來表示你的數(shù)據(jù)庫名稱 // mongoose會自動將大寫名詞的字符串生成小寫復(fù)數(shù)的集合名稱 // 例如:這里的User最終會變?yōu)閡sers集合名稱 // 第二個參數(shù): 架構(gòu)Schema // 返回值: 模型構(gòu)造函數(shù) var User = mongoose.model('User', userSchema)// 4.當我們有了模型構(gòu)造函數(shù)之后,就可以使用這個構(gòu)造函數(shù)對users集合中的數(shù)據(jù)進行操作 // 新增數(shù)據(jù)操作 /* var admin = new User({username: 'zepzep',password: '000000',email: 'zep@zep.com' })admin.save(function(err, ret) {if(err) {console.log('保存失敗')} else {console.log('保存成功!')console.log(ret)} }) */// 查詢數(shù)據(jù)操作 // 1.查詢所有數(shù)據(jù) /* User.find(function(err, ret) {if(err) {console.log('查詢失敗')} else {console.log(ret)} }) */// 2.按條件查詢數(shù)據(jù),返回值為數(shù)組 /* User.find({username: 'zepzep' }, function(err, ret) {if (err) {console.log('查詢失敗')} else {console.log(ret)} })*/// 3.按條件查詢數(shù)據(jù),返回值為查找到的匹配的第一個的對象 /* User.findOne({username: 'zepzep',password: '111' }, function(err, ret) {if (err) {console.log('查詢失敗')} else {console.log(ret)} }) */// 刪除數(shù)據(jù)操作 User.remove({username: 'zepzep' }, function(err, ret) {if(err) {console.log('刪除失敗')} else {console.log('刪除成功!')console.log(ret)} })5.更新數(shù)據(jù)
// 1.導(dǎo)包 const mongoose = require('mongoose')const Schema = mongoose.Schema// 2.連接數(shù)據(jù)庫 // 這里指定連接的數(shù)據(jù)庫mydb可以不需要存在,當你插入第一條數(shù)據(jù)之后就會幫你自動創(chuàng)建這個數(shù)據(jù)庫 mongoose.connect('mongodb://localhost/mydb')// 3.設(shè)計集合結(jié)構(gòu)(表結(jié)構(gòu)) // 字段名稱就是表結(jié)構(gòu)中的屬性名稱 // 值 const userSchema = new Schema({username: {type: String,require: true // 必須有該屬性},password: {type: String,require: true},email: {type: String} });// 3.將文檔結(jié)構(gòu)發(fā)布為模型 // mongoose.model() 就是用來將一個架構(gòu)發(fā)布為 model // 第一個參數(shù): 傳入一個大寫名詞單數(shù)字符串,用來表示你的數(shù)據(jù)庫名稱 // mongoose會自動將大寫名詞的字符串生成小寫復(fù)數(shù)的集合名稱 // 例如:這里的User最終會變?yōu)閡sers集合名稱 // 第二個參數(shù): 架構(gòu)Schema // 返回值: 模型構(gòu)造函數(shù) var User = mongoose.model('User', userSchema)// 4.當我們有了模型構(gòu)造函數(shù)之后,就可以使用這個構(gòu)造函數(shù)對users集合中的數(shù)據(jù)進行操作 // 新增數(shù)據(jù)操作 /* var admin = new User({username: 'zepzep',password: '000000',email: 'zep@zep.com' })admin.save(function(err, ret) {if(err) {console.log('保存失敗')} else {console.log('保存成功!')console.log(ret)} }) */// 查詢數(shù)據(jù)操作 // 1.查詢所有數(shù)據(jù) /* User.find(function(err, ret) {if(err) {console.log('查詢失敗')} else {console.log(ret)} }) */// 2.按條件查詢數(shù)據(jù),返回值為數(shù)組 /* User.find({username: 'zepzep' }, function(err, ret) {if (err) {console.log('查詢失敗')} else {console.log(ret)} })*/// 3.按條件查詢數(shù)據(jù),返回值為查找到的匹配的第一個的對象 /* User.findOne({username: 'zepzep',password: '111' }, function(err, ret) {if (err) {console.log('查詢失敗')} else {console.log(ret)} }) */// 刪除數(shù)據(jù)操作 /* User.remove({username: 'zepzep' }, function(err, ret) {if(err) {console.log('刪除失敗')} else {console.log('刪除成功!')console.log(ret)} }) */// 更新數(shù)據(jù)操作 User.findByIdAndUpdate('6062cff077b662686460e549', {password: '123' }, function(err, ret) {if(err) {console.log('更新失敗')} else {console.log('更新成功!')console.log(ret)} })總結(jié)
以上是生活随笔為你收集整理的使用mongoose 在 Node中操作MongoDB数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: react-router的使用(一)——
- 下一篇: python 私有属性_Python3伪