mongodb基本語法

參考鏈接 https://www.tutorialspoint.com/mongodb/index.htm

一.創(chuàng)建和刪除數(shù)據(jù)庫

1.查看數(shù)據(jù)庫列表

>show dbs
admin  0.000GB
local  0.000GB

2.使用名稱為newdb的數(shù)據(jù)庫,沒有則自動創(chuàng)建

>use newdb
switched to db newdb

3.查看當(dāng)前數(shù)據(jù)庫,db代表當(dāng)前使用的數(shù)據(jù)庫

>db
newdb

4.查看當(dāng)前數(shù)據(jù)庫列表,剛剛創(chuàng)建的newdb并不在列表中,需要至少插入一個集合

>show dbs
admin  0.000GB
local  0.000GB

5.當(dāng)前數(shù)據(jù)庫下創(chuàng)建名稱為users的集合

> db.createCollection("users")
{ "ok" : 1 }

6.此時查看數(shù)據(jù)庫列表,已經(jīng)顯示剛剛創(chuàng)建的數(shù)據(jù)庫newdb

> show dbs
admin  0.000GB
local  0.000GB
newdb  0.000GB

7.刪除當(dāng)前數(shù)據(jù)庫

> db.dropDatabase()
{ "dropped" : "newdb", "ok" : 1 }

8.查看數(shù)據(jù)庫列表,newdb已經(jīng)成功刪除

> show dbs
admin  0.000GB
local  0.000GB
二.數(shù)據(jù)集合的創(chuàng)建與刪除

1.創(chuàng)建名為mycollection的集合到test數(shù)據(jù)庫

> use test
switched to db test
> db.createCollection("mycollection")
{ "ok" : 1 }

2.顯示集合列表

> show collections
mycollection

3.再創(chuàng)建一個

> db.createCollection("articles")
{ "ok" : 1 }
> show collections
articles
mycollection

4.刪除名為articles的集合

> db.articles.drop()
true
> show collections
mycollection

5.再刪除一個

> db.mycollection.drop()
true
> show collections

6.此時集合為空,數(shù)據(jù)庫test不會在列表中顯示

> db
test
> show dbs
admin  0.000GB
local  0.000GB
三.文檔的插入
語法
>db.COLLECTION_NAME.insert(document)

1.在mycol的集合中插入一條數(shù)據(jù),如果沒有mycol的集合,則自動創(chuàng)建,并插入數(shù)據(jù)

> use test
switched to db test
> db.mycol.insert({"name":"馬里奧",age:28})
WriteResult({ "nInserted" : 1 })
四.文檔的查詢
語法
>db.COLLECTION_NAME.find()

1.查看所有數(shù)據(jù),_id為自動生成

> db.mycol.find({})
{ "_id" : ObjectId("5abc4ae93e7971325d6c6617"), "name" : "馬里奧", "age" : 28 }

2.再插入兩條數(shù)據(jù)

> db.mycol.insert({"name":"湯姆",age:2})
WriteResult({ "nInserted" : 1 })
> db.mycol.insert({"name":"杰瑞",age:99})
WriteResult({ "nInserted" : 1 })

3.查看所有數(shù)據(jù)

> db.mycol.find({})
{ "_id" : ObjectId("5abc4ae93e7971325d6c6617"), "name" : "馬里奧", "age" : 28 }
{ "_id" : ObjectId("5abc4bcb3e7971325d6c6618"), "name" : "湯姆", "age" : 2 }
{ "_id" : ObjectId("5abc4bea3e7971325d6c6619"), "name" : "杰瑞", "age" : 99 }

4.查看姓名為"馬里奧"的數(shù)據(jù)

> db.mycol.find({"name":"馬里奧"})
{ "_id" : ObjectId("5abc4ae93e7971325d6c6617"), "name" : "馬里奧", "age" : 28 }

5.查看年齡大于30的數(shù)據(jù)

> db.mycol.find({age:{$gt:30}})
{ "_id" : ObjectId("5abc4bea3e7971325d6c6619"), "name" : "杰瑞", "age" : 99 }

6.查看年齡小于30的數(shù)據(jù),其他(小于:$lt,大于:$gt,小于等于:$lte,大于等于:$gte,不等于:$ne)

> db.mycol.find({age:{$lt:30}})
{ "_id" : ObjectId("5abc4ae93e7971325d6c6617"), "name" : "馬里奧", "age" : 28 }
{ "_id" : ObjectId("5abc4bcb3e7971325d6c6618"), "name" : "湯姆", "age" : 2 }

7.查看年齡為1或者年齡為99的數(shù)據(jù)

> db.mycol.find({$or:[{"age":1},{"age":99}]})
{ "_id" : ObjectId("5abc4bea3e7971325d6c6619"), "name" : "杰瑞", "age" : 99 }
五.文檔的更新
語法
>db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

1.將name為"杰瑞"的年齡改為98

> db.mycol.update({"name":"杰瑞"},{$set:{"age":98}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.mycol.find({"name":"杰瑞"})
{ "_id" : ObjectId("5abc4bea3e7971325d6c6619"), "name" : "杰瑞", "age" : 98}
六.文檔的刪除
語法
>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

1.刪除name為"杰瑞"的數(shù)據(jù)

> db.mycol.remove({"name":"杰瑞"})
WriteResult({ "nRemoved" : 1 })
> db.mycol.find({})
{ "_id" : ObjectId("5abc4ae93e7971325d6c6617"), "name" : "馬里奧", "age" : 28 }
{ "_id" : ObjectId("5abc4bcb3e7971325d6c6618"), "name" : "湯姆", "age" : 2 }

2.刪除年齡大于5的數(shù)據(jù)

> db.mycol.remove({age:{$gt:5}})
WriteResult({ "nRemoved" : 1 })
> db.mycol.find({})
{ "_id" : ObjectId("5abc4bcb3e7971325d6c6618"), "name" : "湯姆", "age" : 2 }

3.刪除所有

> db.mycol.remove({})
WriteResult({ "nRemoved" : 1 })
> db.mycol.find({})

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容