今天第一次接觸到MongoDB,學習了簡單的安裝配置及CRUD操作,做個筆記記錄一波。
一、免安裝版配置
1、下載安裝包 >>> https://www.mongodb.com/download-center/community?jmp=docs
2、zip解壓後可重命名,解壓後文件目錄如下
3、在bin同級目錄創建一個空文件夾data,作爲數據存放路徑
4、在bin同級目錄創建一個文件夾logs,並在文件夾中創建一個mongo.log文件,存放日誌信息
5、在bin同級目錄創建一個mongo.conf配置文件,內容如下
#數據庫存放路徑
dbpath=C:\SoftWare\mongodb4.0.12\data
#日誌輸出文件路徑
logpath=C:\SoftWare\mongodb4.0.12\logs\mongo.log
#錯誤日誌採用追加模式
logappend=true
#啓用日誌文件,默認啓用
journal=true
#過濾無用的日誌信息,若需要調試使用請設置爲false
quiet=true
#端口號 默認爲27017
port=27017
6、在bin同級目錄創建批處理文件startup-conf.bat、startup-dbpath.bat,內容如下
startup-conf.bat
mongod --config "C:\SoftWare\mongodb4.0.12\mongo.conf"
startup-dbpath.bat
mongod --dbpath C:\SoftWare\mongodb4.0.12\data
7、創建完成後完整目錄如下
8、啓動順序:雙擊startup-conf.bat(此窗口關閉),然後雙擊startup-dbpath.bat(注意此窗口不要關閉),然後在C:\SoftWare\mongodb4.0.12\bin目錄下重新開啓一個cmd窗口,執行mongo命令即可啓動成功
注:可不使用批處理啓動,可直接開啓cmd順序執行對應命令即可。可把C:\SoftWare\mongodb4.0.12\bin配置到環境變量,即可全局使用mongo命令。
二、簡單CRUD操作
MongoDB基礎概念解析,對比一下便於理解:
通過下圖實例,可以更直觀的的瞭解Mongo中的一些概念:
大概瞭解清楚了,開始啓動--->進行簡單的MongoDB操作(此處未進行其他用戶密碼等配置)
1、查看當前數據庫
db
2、查看所有數據
#查看所有數據
db.test.find()
#查看第一條數據
db.test.findOne()
#條件查詢
db.test.find({"no":1}) ##select * from db.test where no=1
db.test.find({"no":{"$ne":1}}) ##select * from db.test where no!=1
db.test.find({"no":{"$lt":2}}) ##select * from db.test where no<2
db.test.find({"no":{"$gt":2}}) ##select * from db.test where no>2
db.test.find({"no":{"$gte":2}}) ##select * from db.test where no>=2
db.test.find({"no":{"$lte":2}}) ##select * from db.test where no<=2
#多條件查詢and
db.test.find({"no":{"$ne":1},"age":{"$gt":22}}) ##select * from db.test where no!=1 and age>22
#多條件查詢or
db.test.find({$or:[{"no":{"$lt":2}},{"age":{"$gt":22}}]}) ##select * from db.test where no<2 or age>22
3、新增數據
db.test.insert({"no":5,"name":"zhanggong","age":22})
4、修改數據
db.test.update({"no":1},{"name":"xinxin","age":25})
5、刪除數據
#刪除多箇中的一個
db.test.deleteOne({"no":5})
#刪除多個
db.test.deleteMany({"no":5})
#刪除全部
db.test.deleteMany({})
好了,簡單的增刪改查就這樣,下一章繼續學習MongoDB的基本操作 >>> ~,~