1. 程式人生 > 實用技巧 >MongoDB的常規操作

MongoDB的常規操作

MongoDB的常規操作

MongoDB的命令操作流程:

先進入MongoDB資料庫

首先檢視所有資料庫:

show dbs

1、建立demo資料庫:
use demo

檢視當前資料庫:db
檢視所有資料庫:show dbs

因為demo還沒有資料,所有無法顯示出來

2、建立student集合:
db.createCollection("student")
db.createCollection("test")用於刪除集合的時候檢視資料有對比
查詢集合:
show collections
show tables

demo資料庫有一個以上集合或其他資料之後,就可以查詢到了

3、插入資料:學號、姓名、性別、年齡、備註
db.student.insert({"Sno":15,"Sname":"陳志鋒","Ssex":"男","Sage":21,"Sbz":"創造bug"})
db.student.insert({"Sno":16,"Sname":"鍾靚仔","Ssex":"女","Sage":22,"Sbz":"尋找bug"})

db.student.insert({"Sno":17,"Sname":"賴靚仔","Ssex":"男","Sage":23,"Sbz":"解決bug"})
db.student.insert({"Sno":18,"Sname":"朱靚仔","Ssex":"女","Sage":24,"Sbz":"檢查bug"})
db.student.insert({"Sno":19,"Sname":"黃靚仔","Ssex":"男","Sage":25,"Sbz":"稽核專案"})

4、查詢資料:
檢視所有資料庫:show dbs
檢視集合所以資料:db.student.find()
根據學號索引來查詢:db.student.find({"Sno":15})

5、修改資料:
db.student.update({"Sno":16},{$set:{"Ssex":"男"}})
db.student.find({"Sno":16})
db.student.update({"Sno":18},{$set:{"Ssex":"男"}})
db.student.find()

6、刪除資料
①刪除個別文件:
db.student.remove({"Sno":17})
db.student.find()
②刪除整個集合:
db.student.drop()
show tables
③刪除資料庫:
db.createCollection("student")
db.dropDatabase()

參考:https://www.cnblogs.com/zhif97/p/12806308.html