您好,登錄后才能下訂單哦!
安裝方式
采用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模式
> 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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。