MongoDB教學(一)_下載安裝_基礎指令

 
記得很久以前寫過Redis的文章
和Redis一樣屬於NoSQL種類DB的mongodb



MongoDB官網
https://www.mongodb.com/

是採用json doc 形式的資料庫
無Relation的觀念
因此也不需要預先定義好Schema



Included with your free cloud database:
  1. 512 MB of Storage
  2. Shared RAM
  3. Highly available replica sets, end-to-end encryption, automated patches, REST API


Additionally, get access to the following when you launch a dedicated cluster:
  1. 10 GB or more of storage
  2. Dedicated RAM
  3. Performance optimization tools
  4. Backups & point-in-time recovery
  5. Enterprise security features including encryption key management, LDAP integration, and granular database auditing
  6. Global Clusters


電腦必須要64bit
(MongoDB ver 2.2 之後就不再支持 Windows XP 系统。也無 32bit 的安裝文件維護在官網了)

載點:

這裡使用community版本



選custom可以自己決定安裝在哪
(預設C:\Program Files\MongoDB\Server\4.4.3 )



MongoDB安裝過程就要先指定Data及Log存放的資料夾位置。
ver4之後督添加可以做為Network Service user




勾消[Install MongoDB Compass](先不安裝MongoDB Compass圖形管理工具)








由於4.x之後已經自動設置好

在過去安裝3.x版本還要執行
mongod --directoryperdb --dbpath C:\mongodb\data\db --logpath c:\mongodb\log\mongodb.log --logappend --rest --install


4.X之後有的指令參數也有被拿掉
Error parsing command line : unrecognized option '--rest'


MongoDB 3.2-RC3 : Error parsing command line : unrecognised command 'enableEncryption'

記得cmd要用系統管理員執行避免權限不足



此時再執行mongo.exe就會進入到mongo shell了


在MongoDB資料庫設計概念中
「一個DB會包含多個集合」(PS:集合可以看做是關聯式資料庫中的多張table)



db                                                #顯示目前操作的database(預設是test)
use DATABASE_NAME           #創建或切換DB指令(有大小些區別)
show dbs                                     #顯示DB(和相應空間用量)
show collections                         #顯示集合
db.createCollection(‘users’)       #建立users集合



這裡建立一個customers的db




這裡嘗試建立一個權限管理的DB  "nodeauth"
在底下創建一集合 "users"



這裡我們塞一些資料
db.users.insert({name: 'Ted' , email:'ted33@gmail.com',username:'Teddy',password:'1234'});
db.users.insert({name: 'Jack Wang' , email:'jack466@gmail.com',username:'Jack',password:'5678'});
查看塞入後結果主要用
db.集合名.find()
db.集合名.find().pretty()

插入(新增)指令
db.users.insert({屬性1: '值1' , 屬性2: '值2',屬性3: '值3', 屬性4: '值4' .... });



當然在某些時候會有欄位資料要更新的部分

案例可能類似

Teddy 不小心把email打錯想換過之類的
這裡用username來更新
db.users.update({username:'Teddy'},{$set:{email :'ted5566@yahoo.com'}});

在輸入指令時候依定要注意格式
更新指令
db.集合名稱.update({屬性1:'值1'},{$set:{屬性2 :'值2'}});



這裡在新增一個username為 Micky的
db.users.insert({name: 'Ted' , email:'mike8591@gmail.com',username:'Micky',password:'5566'});


然後我們的刪除指令如下
db.users.remove({屬性1:'值1'});

db.users.remove({username:'Jack'});
嘗試刪除username 為Jack的資料
在查詢出來看確認是有被刪除的

db.users.remove({name:'Ted'});
但如果此時我們拿剩餘兩筆name都被取叫做Ted的來刪除

就會刪光變空了 因為符合到兩筆!!


以上就是基礎指令介紹





Ref:
[MongoDb] 常用指令

MongoDB 官方文件學習筆記(一):概述、資料庫、集合、檢視及定容集合


[Database][MongoDB] 新增、修改、查詢、刪除操作 1 ( CRUD operation 1 )

[Database][MongoDB] 新增、修改、查詢、刪除操作 2 ( CRUD operation 2 )


解决 Win10 安装 MongoDB 4.0 无法启动服务的问题( 踩了个大坑)

Windows MongoDB 下載與安裝教學











留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題