MongoDB入門

來源:互聯網
上載者:User

標籤:

Window安裝二進位包http://www.mongodb.org/downloads

建立一個目錄  C:\data\db  

啟動服務    mongod --dbpath C:\data\db 

啟動用戶端(shell)   mongo 

 

基本概念

SQL術語/概念 MongoDB術語/概念 解釋/說明
database database 資料庫
table collection 資料庫表/集合
row document 資料記錄行/文檔           {key: value}
column field 資料欄位/域
index index 索引
table joins   表串連,MongoDB不支援
primary key primary key 主鍵,MongoDB自動將_id欄位設定為主鍵

  

 

資料庫操作( db.help() )

  show dbs  顯示所有資料庫

  db  顯示當前所在資料庫

  use testdb  切換到tesdbt資料庫,沒有則建立testdb資料庫

  db.dropDatabase() 刪除當前所在資料庫

 

 

 

 集合操作( db.mycoll.help() )

  show collections 顯示所有集合

  db.createCollection(name, { size : ..., capped : ..., max : ... } )  建立集合

     db.coll.insert({‘xx‘:‘xx‘,...})  向coll集合中插入文檔,coll不存在則建立

     db.coll.find()  查看coll集合中的所有文檔,find(m,n)可以指定查詢個數,find().sort() 可以串連使用,還有其他的.size(), .count()...

  db.coll.drop()  刪除coll集合

 

 

用Python操作MongoDB

pip install pymongo

import pymongoclient = pymongo.MongoClient()   可以傳遞參數 (‘localhost‘, 27017)  or  (‘mongod://localhost:21017/‘)db = client[‘testdb‘]     or  client.testdbposts = db[‘posts‘]       or db.postsposts.insert({‘xx‘:‘xx‘, ...})   posts.find()     

 

 

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.