MongoDB是面向集合的文檔式數(shù)據(jù)庫(kù),不像關(guān)系數(shù)據(jù)庫(kù)那樣,有表,列、行,mongoDB數(shù)據(jù)庫(kù)則是由一系列的文檔組成。下面給大家介紹MongoDB的概念及簡(jiǎn)單操作.
1、以下列舉普通的關(guān)系型數(shù)據(jù)庫(kù)和MongoDB數(shù)據(jù)庫(kù)簡(jiǎn)單概念上的區(qū)別:

2、MongoDB的簡(jiǎn)單操作
(1)啟動(dòng)MongoDB數(shù)據(jù)庫(kù)之后,使用命令mongo,顯示如下,默認(rèn)連接到test數(shù)據(jù)庫(kù)。
MongoDB shell version: 3.2.6
connecting to: test
使用命令show dbs,可以查看所有的數(shù)據(jù)庫(kù),可以看見(jiàn)只有一個(gè)local數(shù)據(jù),其實(shí)test數(shù)據(jù)庫(kù)并不存在,只有再建集合并往集合插入數(shù)據(jù)時(shí)才會(huì)真正的建表。
常用命令:
show dbs 顯示所有的數(shù)據(jù)庫(kù)
use 數(shù)據(jù)庫(kù)名 切換到某一個(gè)數(shù)據(jù)中
show collections 顯示當(dāng)前數(shù)據(jù)庫(kù)中所有的集合
db.集合名.find() 查詢當(dāng)前數(shù)據(jù)庫(kù)中某一個(gè)集合下所有的數(shù)據(jù)
db.集合名.insert({"鍵": "值", "鍵": "值" ...}) 給當(dāng)前數(shù)據(jù)庫(kù)中某一個(gè)集合添加數(shù)據(jù)
db.集合名.drop() 刪除某一個(gè)集合
db.dropDatabase() 刪除當(dāng)前數(shù)據(jù)庫(kù)
現(xiàn)在我們用以上命令做一個(gè)簡(jiǎn)單的例子:重新建立一個(gè)數(shù)據(jù)zyhtest,并在zyhtest中新建集合student,并往student中插入數(shù)據(jù)。
> use zyhtestswitched to db zyhtest> db.student.insert({"name": "zhangsan", "age": 28})WriteResult({ "nInserted" : 1 })> show dbslocal 0.000GBzyhtest 0.000GB> show collectionsstudent> db.student.find(){ "_id" : ObjectId("5745b8a08dfa492b66e7d397"), "name" : "zhangsan", "age" : 28 }> db.student.drop()true> show dbslocal 0.000GB> db.student.insert({"name": "zhangsan", "age": 28})WriteResult({ "nInserted" : 1 })> show dbslocal 0.000GBzyhtest 0.000GB> show collectionsstudent> db.dropDatabase(){ "dropped" : "zyhtest", "ok" : 1 }> show dbslocal 0.000GB 插入數(shù)據(jù)時(shí),會(huì)自動(dòng)添加一個(gè)主鍵“_id”
以上內(nèi)容是小編給大家介紹的MongoDB快速入門筆記(二)之MongoDB的概念及簡(jiǎn)單操作的相關(guān)知識(shí),希望對(duì)大家有所幫助!
新聞熱點(diǎn)
疑難解答
圖片精選