您好,登錄后才能下訂單哦!
安裝方式
采用apt-get install mongodb命令直接進(jìn)行,采用源碼包安裝也可以
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 #啟動(dòng)MongoDB
hadoop@dblab:/$ mongo? #進(jìn)入MongoDB Shell模式
> use school? ?#切換到shcool數(shù)據(jù)庫(kù),使用時(shí)會(huì)自動(dòng)創(chuàng)建
switched to db school
> db.createCollection('teacher')? ? #創(chuàng)建集合
{ "ok" : 1 }
> show dbs? ?#顯示數(shù)據(jù)庫(kù)列表
local? 0.000GB
school? 0.000GB
> db.student.insert({_id:1,sname:'zhangsan',sage:20})? ?#插入數(shù)據(jù)
WriteResult({ "nInserted" : 1 })
> db.student.insert({_id:2,sname:'lisi',sage:22})? ?#插入數(shù)據(jù)
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 2 })
> use school
switched to db school
> show collections? ?#顯示當(dāng)前數(shù)據(jù)庫(kù)的集合
student
teacher
#查找數(shù)據(jù)
> db.student.find()? ?#查找所有記錄
{ "_id" : 1, "sname" : "lisi", "sage" : 22 }
{ "_id" : 2, "sname" : "lisi", "sage" : 22 }
> db.student.remove({_id: 2})? ? #刪除數(shù)據(jù)
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 }
>?
#修改數(shù)據(jù)
> 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 }
#刪除數(shù)據(jù)
> db.student.remove({sname:'lisi'})
WriteResult({ "nRemoved" : 1 })
#刪除集合
> db.student.drop()
> show collections
teacher
> exit? #退出MongoDB Shell模式
bye
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。