mongodb的集合操作

來源:互聯網
上載者:User

標籤:show   dex   成功   system   索引   style   writer   options   result   

MongoDB 建立集合

1.手動建立:

文法格式:

db.createCollection(name, options)

參數說明:

  • name: 要建立的集合名稱
  • options: 選擇性參數, 指定有關記憶體大小及索引的選項

options 可以是如下參數:

 

欄位 類型 描述
capped 布爾 (可選)如果為 true,則建立固定集合。固定集合是指有著固定大小的集合,當達到最大值時,它會自動覆蓋最早的文檔。
當該值為 true 時,必須指定 size 參數。
autoIndexId 布爾 (可選)如為 true,自動在 _id 欄位建立索引。預設為 false。
size 數值 (可選)為固定集合指定一個最大值(以位元組計)。
如果 capped 為 true,也需要指定該欄位。
max 數值 (可選)指定固定集合中包含文檔的最大數

 

 例如:在exam表中建立colle1集合:

> use examswitched to db exam> db.createCollection("colle1"){ "ok" : 1 }> show collections #查看當前資料庫的集合colle1system.indexes

 

建立固定集合 mycol,整個集合空間大小 6142800 KB, 文檔最大個數為 10000 個。

> db.createCollection("mycol", { capped : true, autoIndexId : true, size :    6142800, max : 10000 } ){ "ok" : 1 }>

 

 

 

2.在 MongoDB 中,你不需要建立集合。當你插入一些文檔時,MongoDB 會自動建立集合。

 

> show collectionscolle1system.indexes> db.exam.insert({"name111" : "菜鳥教程"})WriteResult({ "nInserted" : 1 })> show collectionscolle1examsystem.indexes
 MongoDB 刪除集合

文法格式:

db.collection.drop()

傳回值

如果成功刪除選定集合,則 drop() 方法返回 true,否則返回 false。

 

 

例如:查看所有的集合并刪除其中一個集合

> show collectionscolle1examsystem.indexes> show tablescolle1examsystem.indexes> db.colle1.drop()true> show tablesexamsystem.indexes>

 

   查看當前資料的集合可以用show collections或者show tables

 

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.