網站首頁 編程語言 正文
一、創建集合
本章節我們為大家介紹如何使用 MongoDB 來創建集合。
MongoDB 中使用?createCollection()?方法來創建集合。
語法格式:
db.createCollection(name, options)
參數說明:
- name: 要創建的集合名稱
- options: 可選參數, 指定有關內存大小及索引的選項
options 可以是如下參數:
在插入文檔時,MongoDB 首先檢查固定集合的 size 字段,然后檢查 max 字段。
實例
在 test 數據庫中創建 runoob 集合:
> use test
switched to db test
> db.createCollection("runoob")
{ "ok" : 1 }
>
如果要查看已有集合,可以使用 show collections 或 show tables 命令:
> show collections
runoob
system.indexes
下面是帶有幾個關鍵參數的 createCollection() 的用法:
創建固定集合 mycol,整個集合空間大小 6142800 KB, 文檔最大個數為 10000 個。
> db.createCollection("mycol", { capped : true, autoIndexId : true, size :
6142800, max : 10000 } )
{ "ok" : 1 }
>
在 MongoDB 中,你不需要創建集合。當你插入一些文檔時,MongoDB 會自動創建集合。
> db.mycol2.insert({"name" : "菜鳥教程"})
> show collections
mycol2
...
二、刪除集合
本章節我們為大家介紹如何使用 MongoDB 來刪除集合。
MongoDB 中使用 drop() 方法來刪除集合。
語法格式:
db.collection.drop()
返回值
如果成功刪除選定集合,則 drop() 方法返回 true,否則返回 false。
實例
在數據庫 mydb 中,我們可以先通過 show collections 命令查看已存在的集合:
>use mydb
switched to db mydb
>show collections
mycol
mycol2
system.indexes
runoob
>
接著刪除集合 mycol2 :
>db.mycol2.drop()
true
>
通過 show collections 再次查看數據庫 mydb 中的集合:
>show collections
mycol
system.indexes
runoob
>
從結果中可以看出 mycol2 集合已被刪除。
原文鏈接:https://www.cnblogs.com/springsnow/p/12931958.html
相關推薦
- 2022-08-01 混淆矩陣Confusion?Matrix概念分析翻譯_其它綜合
- 2022-03-22 C++對象與繼承使用中一些問題介紹_C 語言
- 2022-12-26 C++逆向分析移除鏈表元素實現方法詳解_C 語言
- 2023-02-01 Python繪制正二十面體圖形示例_python
- 2022-04-06 Python的type函數結果你知道嘛_python
- 2022-09-30 Python學習之pip包管理工具的使用_python
- 2022-10-24 Django使用Redis進行緩存詳細步驟_Redis
- 2022-08-19 InnoDB 事務
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支