網站首頁 編程語言 正文
一、創建集合
本章節我們為大家介紹如何使用 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-05-02 Entity?Framework中執行sql語句_實用技巧
- 2022-10-01 iOS實現UIButton的拖拽功能_IOS
- 2022-04-19 Windows中Python上傳文件到Liunx下的fastdfs
- 2022-07-10 詳解HashMap并發修改異常
- 2022-07-07 python中列表對象pop()方法的使用說明_python
- 2023-07-07 什么是 AOP?對于 Spring IoC 和 AOP 的理解?
- 2022-05-22 Nginx安裝后常用功能配置基礎篇_nginx
- 2022-07-28 詳解Python中4種超參自動優化算法的實現_python
- 最近更新
-
- 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同步修改后的遠程分支