MongoDB基礎--資料庫和集合基本操作

來源:互聯網
上載者:User

標籤:

    本筆記針對https://www.shiyanlou.com/courses/running/50 網站對mongoDB的學習和總結。1,啟動mongoDB    因為mongoDB服務並不隨著系統一起啟動,所以需要首先啟動mongoDB服務:
  1. shiyanlou:~/ $ sudo service mongodb start

     

2,資料庫簡介
  •     一個MongoDB可以建立多個資料庫。
  •     使用 show dbs可以查看所有資料庫的列表
  •     執行db命令則可以查看當前資料庫物件或者集合。
  •     運行use命令則可以串連到指定的資料庫
串連資料庫:
  1. shiyanlou:~/ $ mongo> show dbslocal0.078125GB
-1,文檔    文檔是mongoDB的核心,類似於關係型資料庫的一行資料。多個鍵以及其關聯的值放到一起就是文檔。在mongoDB中使用一種類似JSON格式的bson儲存資料。bson可以再json基礎上添加一些json不具備的資料類型。如:
  1. {"company":"Chenshi keji"}
-2,文檔邏輯關係:
  1. 內嵌關係:A文檔中存在B文檔。
  2.     引用式關係:兩個文檔分卡,A文檔通過引用B文檔的ID來實現關聯。
-3,集合    集合就是一組文檔的組合,相當於關聯式資料庫中的表。
  1. {"company":"Chenshi keji"}{"people":"man","name":"peter"}
-4,中繼資料    資料庫資訊儲存在集合中,他們統一使用系統的命名空間    DBNAME.system.* DBNAME可以用db或資料庫名代替。
  1. DBNAME.system.namespaces :列出所有名字空間DBNAME.system.indexs :列出所有索引DBNAME.system.profile :列出資料庫概要資訊DBNAME.system.users :列出訪問資料庫的使用者DBNAME.system.sources :列出伺服器資訊

     

2,資料庫的建立和銷毀    -1,建立資料庫使用use命令床架資料庫
  1. use mydb
查看當前串連的資料庫
  1. db
查看所有的資料庫
  1. show dbs
列出的資料庫中不顯示mydb,是因為mydb為空白。    -2,銷毀資料庫。
  1. > db.dropDatabase(){"dropped":"local","ok":1}
3,集合的建立和銷毀。    -1,建立集合
  1. > use mydbswitched to db mydb> db.createCollection("users"){"ok":1}>
    查看集合
  1. > show collectionssystem.indexesusers>
    -2,刪除集合
  1. > db.users.drop()true>
4,向集合中插入資料    insert 插入資料時,如果users集合沒有建立會自動建立。
  1. > db.users.insert([{name:‘jam‘,email:‘[email protected]‘},{name:‘tom‘, email:‘[email protected]q.email‘}])
    save  插入資料時,如果users集合沒有建立會自動建立。
  1. > db.users.save([{name:‘jam‘,email:‘[email protected]‘},{name:‘tom‘, email:‘[email protected]q.email‘}])

     

來自為知筆記(Wiz)

 

MongoDB基礎--資料庫和集合基本操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.