典型NoSQL數據庫的安裝和使用——MongoDB安裝和使用

安裝方式

採用apt-get install mongodb命令直接進行,採用源碼包安裝也可以

hadoop@dblab:/$ sudo apt-get update

hadoop@dblab:/$ sudo apt-get install -y mongodb-org

hadoop@dblab:/$ mongo -version

MongoDB shell version: 3.2.22

hadoop@dblab:/$ sudo service mongodb start #啓動MongoDB

hadoop@dblab:/$ mongo  #進入MongoDB Shell模式image.png

> use school   #切換到shcool數據庫,使用時會自動創建

switched to db school

> db.createCollection('teacher')    #創建集合

{ "ok" : 1 }

> show dbs   #顯示數據庫列表

local  0.000GB

school  0.000GB

> db.student.insert({_id:1,sname:'zhangsan',sage:20})   #插入數據

WriteResult({ "nInserted" : 1 })

> db.student.insert({_id:2,sname:'lisi',sage:22})   #插入數據

WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 2 })

> use school

switched to db school

> show collections   #顯示當前數據庫的集合

student

teacher

#查找數據

> db.student.find()   #查找所有記錄

{ "_id" : 1, "sname" : "lisi", "sage" : 22 }

{ "_id" : 2, "sname" : "lisi", "sage" : 22 }

> db.student.remove({_id: 2})    #刪除數據

WriteResult({ "nRemoved" : 1 })

> db.student.find()

{ "_id" : 1, "sname" : "lisi", "sage" : 22 }

> db.student.insert({_id:2,sname:'zhangsan',sage:25})

WriteResult({ "nInserted" : 1 })

> db.student.find()

{ "_id" : 1, "sname" : "lisi", "sage" : 22 }

{ "_id" : 2, "sname" : "zhangsan", "sage" : 25 }

#修改數據

> db.student.update({_id:2},{$set:{sage:88}},false,true)

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

> db.student.find().pretty()

{ "_id" : 1, "sname" : "lisi", "sage" : 22 }

{ "_id" : 2, "sname" : "zhangsan", "sage" : 88 }

#刪除數據

> db.student.remove({sname:'lisi'})

WriteResult({ "nRemoved" : 1 })

#刪除集合

> db.student.drop()

> show collections

teacher

> exit  #退出MongoDB Shell模式

bye

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章