MongoDB是面向集合的文檔式數(shù)據(jù)庫,不像關(guān)系數(shù)據(jù)庫那樣,有表,列、行,mongoDB數(shù)據(jù)庫則是由一系列的文檔組成。下面給大家介紹MongoDB的概念及簡單操作.
1、以下列舉普通的關(guān)系型數(shù)據(jù)庫和MongoDB數(shù)據(jù)庫簡單概念上的區(qū)別:
2、MongoDB的簡單操作
(1)啟動MongoDB數(shù)據(jù)庫之后,使用命令mongo,顯示如下,默認(rèn)連接到test數(shù)據(jù)庫。
MongoDB shell version: 3.2.6
connecting to: test
使用命令show dbs,可以查看所有的數(shù)據(jù)庫,可以看見只有一個local數(shù)據(jù),其實test數(shù)據(jù)庫并不存在,只有再建集合并往集合插入數(shù)據(jù)時才會真正的建表。
常用命令:
show dbs 顯示所有的數(shù)據(jù)庫
use 數(shù)據(jù)庫名 切換到某一個數(shù)據(jù)中
show collections 顯示當(dāng)前數(shù)據(jù)庫中所有的集合
db.集合名.find() 查詢當(dāng)前數(shù)據(jù)庫中某一個集合下所有的數(shù)據(jù)
db.集合名.insert({"鍵": "值", "鍵": "值" ...}) 給當(dāng)前數(shù)據(jù)庫中某一個集合添加數(shù)據(jù)
db.集合名.drop() 刪除某一個集合
db.dropDatabase() 刪除當(dāng)前數(shù)據(jù)庫
現(xiàn)在我們用以上命令做一個簡單的例子:重新建立一個數(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ù)時,會自動添加一個主鍵“_id”
以上內(nèi)容是小編給大家介紹的MongoDB快速入門筆記(二)之MongoDB的概念及簡單操作的相關(guān)知識,希望對大家有所幫助!
新聞熱點(diǎn)
疑難解答